SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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.sourceforge.net/
12 // Copyright (C) 2001-2012 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  // read in edge attributes
119  std::string id = useRunningID ? toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
120  ++runningID;
121  id = StringUtils::prune(id);
122  if (id == "") {
123  throw ProcessError("Missing id under '" + idField + "'");
124  }
125  id = prefix + id;
126  // read in the geometry
127  OGRGeometry* poGeometry = poFeature->GetGeometryRef();
128  if (poGeometry != 0) {
129  // try transform to wgs84
130  poGeometry->transform(poCT);
131  }
132  OGRwkbGeometryType gtype = poGeometry->getGeometryType();
133  switch (gtype) {
134  case wkbPoint: {
135  OGRPoint* cgeom = (OGRPoint*) poGeometry;
136  Position pos((SUMOReal) cgeom->getX(), (SUMOReal) cgeom->getY());
137  if (!geoConvHelper.x2cartesian(pos)) {
138  WRITE_ERROR("Unable to project coordinates for POI '" + id + "'.");
139  }
140  PointOfInterest* poi = new PointOfInterest(id, type, color, pos, (SUMOReal)layer);
141  if (!toFill.insert(id, poi, layer)) {
142  WRITE_ERROR("POI '" + id + "' could not be added.");
143  delete poi;
144  }
145  }
146  break;
147  case wkbLineString: {
148  OGRLineString* cgeom = (OGRLineString*) poGeometry;
149  PositionVector shape;
150  for (int j = 0; j < cgeom->getNumPoints(); j++) {
151  Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
152  if (!geoConvHelper.x2cartesian(pos)) {
153  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
154  }
155  shape.push_back_noDoublePos(pos);
156  }
157  Polygon* poly = new Polygon(id, type, color, shape, false, (SUMOReal)layer);
158  if (!toFill.insert(id, poly, layer)) {
159  WRITE_ERROR("Polygon '" + id + "' could not be added.");
160  delete poly;
161  }
162  }
163  break;
164  case wkbPolygon: {
165  OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
166  PositionVector shape;
167  for (int j = 0; j < cgeom->getNumPoints(); j++) {
168  Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
169  if (!geoConvHelper.x2cartesian(pos)) {
170  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
171  }
172  shape.push_back_noDoublePos(pos);
173  }
174  Polygon* poly = new Polygon(id, type, color, shape, true, (SUMOReal)layer);
175  if (!toFill.insert(id, poly, layer)) {
176  WRITE_ERROR("Polygon '" + id + "' could not be added.");
177  delete 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  WRITE_ERROR("POI '" + tid + "' could not be added.");
193  delete poi;
194  }
195  }
196  }
197  break;
198  case wkbMultiLineString: {
199  OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
200  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
201  OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
202  PositionVector shape;
203  std::string tid = id + "#" + toString(i);
204  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
205  Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
206  if (!geoConvHelper.x2cartesian(pos)) {
207  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
208  }
209  shape.push_back_noDoublePos(pos);
210  }
211  Polygon* poly = new Polygon(tid, type, color, shape, false, (SUMOReal)layer);
212  if (!toFill.insert(tid, poly, layer)) {
213  WRITE_ERROR("Polygon '" + tid + "' could not be added.");
214  delete poly;
215  }
216  }
217  }
218  break;
219  case wkbMultiPolygon: {
220  OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
221  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
222  OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
223  PositionVector shape;
224  std::string tid = id + "#" + toString(i);
225  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
226  Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
227  if (!geoConvHelper.x2cartesian(pos)) {
228  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
229  }
230  shape.push_back_noDoublePos(pos);
231  }
232  Polygon* poly = new Polygon(tid, type, color, shape, true, (SUMOReal)layer);
233  if (!toFill.insert(tid, poly, layer)) {
234  WRITE_ERROR("Polygon '" + tid + "' could not be added.");
235  delete poly;
236  }
237  }
238  }
239  break;
240  default:
241  WRITE_WARNING("Unsupported shape type occured (id='" + id + "').");
242  break;
243  }
244  OGRFeature::DestroyFeature(poFeature);
245  }
247 #else
248  WRITE_ERROR("SUMO was compiled without GDAL support.");
249 #endif
250 }
251 
252 
253 /****************************************************************************/
254