Eclipse SUMO - Simulation of Urban MObility
PCLoaderArcView.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A reader of pois and polygons from shape files
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
27 #include <utils/common/ToString.h>
30 #include <utils/geom/GeomHelper.h>
32 #include <utils/common/RGBColor.h>
34 #include <polyconvert/PCTypeMap.h>
35 #include "PCLoaderArcView.h"
36 
37 #ifdef HAVE_GDAL
38 #if __GNUC__ > 3
39 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wpedantic"
41 #endif
42 #include <ogrsf_frmts.h>
43 #if __GNUC__ > 3
44 #pragma GCC diagnostic pop
45 #endif
46 #endif
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 void
54  PCTypeMap& tm) {
55  if (!oc.isSet("shapefile-prefixes")) {
56  return;
57  }
58  // parse file(s)
59  std::vector<std::string> files = oc.getStringVector("shapefile-prefixes");
60  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
61  PROGRESS_BEGIN_MESSAGE("Parsing from shape-file '" + *file + "'");
62  load(*file, oc, toFill, tm);
64  }
65 }
66 
67 
68 
69 void
70 PCLoaderArcView::load(const std::string& file, OptionsCont& oc, PCPolyContainer& toFill,
71  PCTypeMap& tm) {
72 #ifdef HAVE_GDAL
74  // get defaults
75  const std::string idField = oc.getString("shapefile.id-column");
76  const bool useRunningID = oc.getBool("shapefile.use-running-id") || idField == "";
77  // start parsing
78  std::string shpName = file + ".shp";
79  int fillType = -1;
80  if (oc.getString("shapefile.fill") == "true") {
81  fillType = 1;
82  } else if (oc.getString("shapefile.fill") == "false") {
83  fillType = 0;
84  }
85 #if GDAL_VERSION_MAJOR < 2
86  OGRRegisterAll();
87  OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
88 #else
89  GDALAllRegister();
90  GDALDataset* poDS = (GDALDataset*) GDALOpenEx(shpName.c_str(), GDAL_OF_VECTOR | GA_ReadOnly, NULL, NULL, NULL);
91 #endif
92  if (poDS == NULL) {
93  throw ProcessError("Could not open shape description '" + shpName + "'.");
94  }
95 
96  // begin file parsing
97  OGRLayer* poLayer = poDS->GetLayer(0);
98  poLayer->ResetReading();
99 
100  // build coordinate transformation
101  OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
102  OGRSpatialReference destTransf;
103  // use wgs84 as destination
104  destTransf.SetWellKnownGeogCS("WGS84");
105  OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
106  if (poCT == NULL) {
107  if (oc.isSet("shapefile.guess-projection")) {
108  OGRSpatialReference origTransf2;
109  origTransf2.SetWellKnownGeogCS("WGS84");
110  poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
111  }
112  if (poCT == 0) {
113  WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed.");
114  }
115  }
116 
117  OGRFeature* poFeature;
118  poLayer->ResetReading();
119  int runningID = 0;
120  while ((poFeature = poLayer->GetNextFeature()) != NULL) {
121  std::vector<Parameterised*> parCont;
122  // read in edge attributes
123  std::string id = useRunningID ? toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
124  ++runningID;
126  if (id == "") {
127  throw ProcessError("Missing id under '" + idField + "'");
128  }
129  id = oc.getString("prefix") + id;
130  std::string type;
131  for (const std::string& typeField : oc.getStringVector("shapefile.type-columns")) {
132  if (type != "") {
133  type += ".";
134  }
135  type += poFeature->GetFieldAsString(typeField.c_str());
136  }
137  RGBColor color = RGBColor::parseColor(oc.getString("color"));
138  double layer = oc.getFloat("layer");
139  double angle = Shape::DEFAULT_ANGLE;
140  std::string imgFile = Shape::DEFAULT_IMG_FILE;
141  if (type != "" && tm.has(type)) {
142  const PCTypeMap::TypeDef& def = tm.get(type);
143  if (def.discard) {
144  continue;
145  }
146  color = def.color;
147  layer = def.layer;
148  angle = def.angle;
149  imgFile = def.imgFile;
150  type = def.id;
151  } else {
152  type = oc.getString("type");
153  }
154  if (poFeature->GetFieldIndex("angle") >= 0) {
155  angle = poFeature->GetFieldAsDouble("angle");
156  }
157  // read in the geometry
158  OGRGeometry* poGeometry = poFeature->GetGeometryRef();
159  if (poGeometry == 0) {
160  OGRFeature::DestroyFeature(poFeature);
161  continue;
162  }
163  // try transform to wgs84
164  poGeometry->transform(poCT);
165  OGRwkbGeometryType gtype = poGeometry->getGeometryType();
166  switch (gtype) {
167  case wkbPoint: {
168  OGRPoint* cgeom = (OGRPoint*) poGeometry;
169  Position pos(cgeom->getX(), cgeom->getY());
170  if (!geoConvHelper.x2cartesian(pos)) {
171  WRITE_ERROR("Unable to project coordinates for POI '" + id + "'.");
172  }
173  PointOfInterest* poi = new PointOfInterest(id, type, color, pos, false, "", 0, 0, layer, angle, imgFile);
174  if (toFill.add(poi)) {
175  parCont.push_back(poi);
176  }
177  }
178  break;
179  case wkbLineString: {
180  OGRLineString* cgeom = (OGRLineString*) poGeometry;
181  PositionVector shape;
182  for (int j = 0; j < cgeom->getNumPoints(); j++) {
183  Position pos(cgeom->getX(j), cgeom->getY(j));
184  if (!geoConvHelper.x2cartesian(pos)) {
185  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
186  }
187  shape.push_back_noDoublePos(pos);
188  }
189  SUMOPolygon* poly = new SUMOPolygon(id, type, color, shape, false, fillType == 1, 1, layer, angle, imgFile);
190  if (toFill.add(poly)) {
191  parCont.push_back(poly);
192  }
193  }
194  break;
195  case wkbPolygon: {
196  const bool fill = fillType < 0 || fillType == 1;
197  OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
198  PositionVector shape;
199  for (int j = 0; j < cgeom->getNumPoints(); j++) {
200  Position pos((double) cgeom->getX(j), (double) cgeom->getY(j));
201  if (!geoConvHelper.x2cartesian(pos)) {
202  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
203  }
204  shape.push_back_noDoublePos(pos);
205  }
206  SUMOPolygon* poly = new SUMOPolygon(id, type, color, shape, false, fill, 1, layer, angle, imgFile);
207  if (toFill.add(poly)) {
208  parCont.push_back(poly);
209  }
210  }
211  break;
212  case wkbMultiPoint: {
213  OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
214  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
215  OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
216  Position pos(cgeom2->getX(), cgeom2->getY());
217  std::string tid = id + "#" + toString(i);
218  if (!geoConvHelper.x2cartesian(pos)) {
219  WRITE_ERROR("Unable to project coordinates for POI '" + tid + "'.");
220  }
221  PointOfInterest* poi = new PointOfInterest(tid, type, color, pos, false, "", 0, 0, layer, angle, imgFile);
222  if (toFill.add(poi)) {
223  parCont.push_back(poi);
224  }
225  }
226  }
227  break;
228  case wkbMultiLineString: {
229  OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
230  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
231  OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
232  PositionVector shape;
233  std::string tid = id + "#" + toString(i);
234  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
235  Position pos(cgeom2->getX(j), cgeom2->getY(j));
236  if (!geoConvHelper.x2cartesian(pos)) {
237  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
238  }
239  shape.push_back_noDoublePos(pos);
240  }
241  SUMOPolygon* poly = new SUMOPolygon(tid, type, color, shape, false, fillType == 1, 1, layer, angle, imgFile);
242  if (toFill.add(poly)) {
243  parCont.push_back(poly);
244  }
245  }
246  }
247  break;
248  case wkbMultiPolygon: {
249  const bool fill = fillType < 0 || fillType == 1;
250  OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
251  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
252  OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
253  PositionVector shape;
254  std::string tid = id + "#" + toString(i);
255  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
256  Position pos(cgeom2->getX(j), cgeom2->getY(j));
257  if (!geoConvHelper.x2cartesian(pos)) {
258  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
259  }
260  shape.push_back_noDoublePos(pos);
261  }
262  SUMOPolygon* poly = new SUMOPolygon(tid, type, color, shape, false, fill, 1, layer, angle, imgFile);
263  if (toFill.add(poly)) {
264  parCont.push_back(poly);
265  }
266  }
267  }
268  break;
269  default:
270  WRITE_WARNING("Unsupported shape type occurred (id='" + id + "').");
271  break;
272  }
273  if (oc.getBool("shapefile.add-param")) {
274  for (std::vector<Parameterised*>::const_iterator it = parCont.begin(); it != parCont.end(); ++it) {
275  OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
276  for (int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
277  OGRFieldDefn* poFieldDefn = poFDefn->GetFieldDefn(iField);
278  if (poFieldDefn->GetNameRef() != idField) {
279  if (poFieldDefn->GetType() == OFTReal) {
280  (*it)->setParameter(poFieldDefn->GetNameRef(), toString(poFeature->GetFieldAsDouble(iField)));
281  } else {
282  (*it)->setParameter(poFieldDefn->GetNameRef(), StringUtils::latin1_to_utf8(poFeature->GetFieldAsString(iField)));
283  }
284  }
285  }
286  }
287  }
288  OGRFeature::DestroyFeature(poFeature);
289  }
290 #if GDAL_VERSION_MAJOR < 2
291  OGRDataSource::DestroyDataSource(poDS);
292 #else
293  GDALClose(poDS);
294 #endif
296 #else
297  UNUSED_PARAMETER(file);
298  UNUSED_PARAMETER(oc);
299  UNUSED_PARAMETER(toFill);
300  UNUSED_PARAMETER(tm);
301  WRITE_ERROR("SUMO was compiled without GDAL support.");
302 #endif
303 }
304 
305 
306 /****************************************************************************/
307 
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
ToString.h
PCTypeMap::TypeDef::imgFile
std::string imgFile
The image file to use.
Definition: PCTypeMap.h:70
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
PCTypeMap::TypeDef::layer
double layer
The layer to use.
Definition: PCTypeMap.h:66
OptionsCont.h
MsgHandler.h
PCTypeMap::TypeDef::discard
bool discard
Information whether polygons of this type shall be discarded.
Definition: PCTypeMap.h:72
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
GeoConvHelper::getProcessing
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:86
GeoConvHelper.h
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
PCTypeMap.h
PositionVector
A list of positions.
Definition: PositionVector.h:45
PCTypeMap::get
const TypeDef & get(const std::string &id)
Returns a type definition.
Definition: PCTypeMap.cpp:70
GeoConvHelper
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:55
GeoConvHelper::x2cartesian
bool x2cartesian(Position &from, bool includeInBoundary=true)
Converts the given coordinate into a cartesian and optionally update myConvBoundary.
Definition: GeoConvHelper.cpp:326
RGBColor.h
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
RGBColor
Definition: RGBColor.h:39
PCPolyContainer::add
bool add(SUMOPolygon *poly, bool ignorePruning=false)
Adds a polygon to the storage.
Definition: PCPolyContainer.cpp:58
PCLoaderArcView.h
PositionVector::push_back_noDoublePos
void push_back_noDoublePos(const Position &p)
insert in back a non double position
Definition: PositionVector.cpp:1295
StringUtils::prune
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:48
RGBColor::parseColor
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:176
PCLoaderArcView::load
static void load(const std::string &file, OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Parses pois/polys stored within the given file.
Definition: PCLoaderArcView.cpp:70
PCPolyContainer.h
ProcessError
Definition: UtilExceptions.h:39
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
PCLoaderArcView::loadIfSet
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as shape files-files.
Definition: PCLoaderArcView.cpp:53
StringUtils::latin1_to_utf8
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
Definition: StringUtils.cpp:70
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
SUMOPolygon
Definition: SUMOPolygon.h:46
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
PROGRESS_BEGIN_MESSAGE
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:278
PCTypeMap::has
bool has(const std::string &id)
Returns the information whether the named type is known.
Definition: PCTypeMap.cpp:76
PROGRESS_DONE_MESSAGE
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:279
PCPolyContainer
A storage for loaded polygons and pois.
Definition: PCPolyContainer.h:50
Shape::DEFAULT_ANGLE
static const double DEFAULT_ANGLE
Definition: Shape.h:46
config.h
PCTypeMap::TypeDef::id
std::string id
The new type id to use.
Definition: PCTypeMap.h:60
GeomHelper.h
PointOfInterest
A point-of-interest.
Definition: PointOfInterest.h:43
PCTypeMap::TypeDef::color
RGBColor color
The color to use.
Definition: PCTypeMap.h:62
Shape::DEFAULT_IMG_FILE
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:47
PCTypeMap
A storage for type mappings.
Definition: PCTypeMap.h:44
PCTypeMap::TypeDef
A single definition of values that shall be used for a given type.
Definition: PCTypeMap.h:58
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
PCTypeMap::TypeDef::angle
double angle
The angle to use.
Definition: PCTypeMap.h:68