SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PCLoaderArcView.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A reader of pois and polygons from shape files
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
35 #include <utils/common/ToString.h>
38 #include <utils/geom/GeomHelper.h>
39 #include "PCLoaderArcView.h"
41 #include <utils/common/RGBColor.h>
43 
44 #ifdef HAVE_GDAL
45 #include <ogrsf_frmts.h>
46 #endif
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 void
58  PCTypeMap& tm) {
59  if (!oc.isSet("shapefile-prefixes")) {
60  return;
61  }
62  // parse file(s)
63  std::vector<std::string> files = oc.getStringVector("shapefile-prefixes");
64  for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
65  PROGRESS_BEGIN_MESSAGE("Parsing from shape-file '" + *file + "'");
66  load(*file, oc, toFill, tm);
68  }
69 }
70 
71 
72 
73 void
74 PCLoaderArcView::load(const std::string& file, OptionsCont& oc, PCPolyContainer& toFill,
75  PCTypeMap&) {
76 #ifdef HAVE_GDAL
78  // get defaults
79  std::string prefix = oc.getString("prefix");
80  std::string type = oc.getString("type");
81  RGBColor color = RGBColor::parseColor(oc.getString("color"));
82  int layer = oc.getInt("layer");
83  std::string idField = oc.getString("shapefile.id-column");
84  bool useRunningID = oc.getBool("shapefile.use-running-id");
85  // start parsing
86  std::string shpName = file + ".shp";
87  OGRRegisterAll();
88  OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
89  if (poDS == NULL) {
90  throw ProcessError("Could not open shape description '" + shpName + "'.");
91  }
92 
93  // begin file parsing
94  OGRLayer* poLayer = poDS->GetLayer(0);
95  poLayer->ResetReading();
96 
97  // build coordinate transformation
98  OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
99  OGRSpatialReference destTransf;
100  // use wgs84 as destination
101  destTransf.SetWellKnownGeogCS("WGS84");
102  OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
103  if (poCT == NULL) {
104  if (oc.isSet("shapefile.guess-projection")) {
105  OGRSpatialReference origTransf2;
106  origTransf2.SetWellKnownGeogCS("WGS84");
107  poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
108  }
109  if (poCT == 0) {
110  WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed.");
111  }
112  }
113 
114  OGRFeature* poFeature;
115  poLayer->ResetReading();
116  unsigned int runningID = 0;
117  while ((poFeature = poLayer->GetNextFeature()) != NULL) {
118  std::vector<Parameterised*> parCont;
119  // read in edge attributes
120  std::string id = useRunningID ? toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
121  ++runningID;
123  if (id == "") {
124  throw ProcessError("Missing id under '" + idField + "'");
125  }
126  id = prefix + id;
127  // read in the geometry
128  OGRGeometry* poGeometry = poFeature->GetGeometryRef();
129  if (poGeometry == 0) {
130  OGRFeature::DestroyFeature(poFeature);
131  continue;
132  }
133  // try transform to wgs84
134  poGeometry->transform(poCT);
135  OGRwkbGeometryType gtype = poGeometry->getGeometryType();
136  switch (gtype) {
137  case wkbPoint: {
138  OGRPoint* cgeom = (OGRPoint*) poGeometry;
139  Position pos((SUMOReal) cgeom->getX(), (SUMOReal) cgeom->getY());
140  if (!geoConvHelper.x2cartesian(pos)) {
141  WRITE_ERROR("Unable to project coordinates for POI '" + id + "'.");
142  }
143  PointOfInterest* poi = new PointOfInterest(id, type, color, pos, (SUMOReal)layer);
144  if (toFill.insert(id, poi, layer)) {
145  parCont.push_back(poi);
146  }
147  }
148  break;
149  case wkbLineString: {
150  OGRLineString* cgeom = (OGRLineString*) poGeometry;
151  PositionVector shape;
152  for (int j = 0; j < cgeom->getNumPoints(); j++) {
153  Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
154  if (!geoConvHelper.x2cartesian(pos)) {
155  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
156  }
157  shape.push_back_noDoublePos(pos);
158  }
159  Polygon* poly = new Polygon(id, type, color, shape, false, (SUMOReal)layer);
160  if (toFill.insert(id, poly, layer)) {
161  parCont.push_back(poly);
162  }
163  }
164  break;
165  case wkbPolygon: {
166  OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
167  PositionVector shape;
168  for (int j = 0; j < cgeom->getNumPoints(); j++) {
169  Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
170  if (!geoConvHelper.x2cartesian(pos)) {
171  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
172  }
173  shape.push_back_noDoublePos(pos);
174  }
175  Polygon* poly = new Polygon(id, type, color, shape, true, (SUMOReal)layer);
176  if (toFill.insert(id, poly, layer)) {
177  parCont.push_back(poly);
178  }
179  }
180  break;
181  case wkbMultiPoint: {
182  OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
183  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
184  OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
185  Position pos((SUMOReal) cgeom2->getX(), (SUMOReal) cgeom2->getY());
186  std::string tid = id + "#" + toString(i);
187  if (!geoConvHelper.x2cartesian(pos)) {
188  WRITE_ERROR("Unable to project coordinates for POI '" + tid + "'.");
189  }
190  PointOfInterest* poi = new PointOfInterest(tid, type, color, pos, (SUMOReal)layer);
191  if (toFill.insert(tid, poi, layer)) {
192  parCont.push_back(poi);
193  }
194  }
195  }
196  break;
197  case wkbMultiLineString: {
198  OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
199  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
200  OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
201  PositionVector shape;
202  std::string tid = id + "#" + toString(i);
203  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
204  Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
205  if (!geoConvHelper.x2cartesian(pos)) {
206  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
207  }
208  shape.push_back_noDoublePos(pos);
209  }
210  Polygon* poly = new Polygon(tid, type, color, shape, false, (SUMOReal)layer);
211  if (toFill.insert(tid, poly, layer)) {
212  parCont.push_back(poly);
213  }
214  }
215  }
216  break;
217  case wkbMultiPolygon: {
218  OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
219  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
220  OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
221  PositionVector shape;
222  std::string tid = id + "#" + toString(i);
223  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
224  Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
225  if (!geoConvHelper.x2cartesian(pos)) {
226  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
227  }
228  shape.push_back_noDoublePos(pos);
229  }
230  Polygon* poly = new Polygon(tid, type, color, shape, true, (SUMOReal)layer);
231  if (toFill.insert(tid, poly, layer)) {
232  parCont.push_back(poly);
233  }
234  }
235  }
236  break;
237  default:
238  WRITE_WARNING("Unsupported shape type occured (id='" + id + "').");
239  break;
240  }
241  if (oc.getBool("shapefile.add-param")) {
242  for (std::vector<Parameterised*>::const_iterator it = parCont.begin(); it != parCont.end(); ++it) {
243  OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
244  for (int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
245  OGRFieldDefn* poFieldDefn = poFDefn->GetFieldDefn(iField);
246  if (poFieldDefn->GetNameRef() != idField) {
247  if (poFieldDefn->GetType() == OFTReal) {
248  (*it)->addParameter(poFieldDefn->GetNameRef(), toString(poFeature->GetFieldAsDouble(iField)));
249  } else {
250  (*it)->addParameter(poFieldDefn->GetNameRef(), StringUtils::latin1_to_utf8(poFeature->GetFieldAsString(iField)));
251  }
252  }
253  }
254  }
255  }
256  OGRFeature::DestroyFeature(poFeature);
257  }
259 #else
260  WRITE_ERROR("SUMO was compiled without GDAL support.");
261 #endif
262 }
263 
264 
265 /****************************************************************************/
266 
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:172
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
Definition: GeoConvHelper.h:98
bool x2cartesian(Position &from, bool includeInBoundary=true)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePruning=false)
Adds a polygon to the storage.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
A storage for loaded polygons and pois.
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
Definition: StringUtils.cpp:78
A 2D- or 3D-polygon.
Definition: Polygon.h:52
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
A storage for type mappings.
Definition: PCTypeMap.h:52
static methods for processing the coordinates conversion for the current net
Definition: GeoConvHelper.h:60
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as shape files-files.
static void load(const std::string &file, OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Parses pois/polys stored within the given file.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:56
A storage for options typed value containers)
Definition: OptionsCont.h:108
#define SUMOReal
Definition: config.h:218
void push_back_noDoublePos(const Position &p)
A point-of-interest.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.