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  // start parsing
85  std::string shpName = file + ".shp";
86  OGRRegisterAll();
87  OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
88  if (poDS == NULL) {
89  throw ProcessError("Could not open shape description '" + shpName + "'.");
90  }
91 
92  // begin file parsing
93  OGRLayer* poLayer = poDS->GetLayer(0);
94  poLayer->ResetReading();
95 
96  // build coordinate transformation
97  OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
98  OGRSpatialReference destTransf;
99  // use wgs84 as destination
100  destTransf.SetWellKnownGeogCS("WGS84");
101  OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
102  if (poCT == NULL) {
103  if (oc.isSet("shapefile.guess-projection")) {
104  OGRSpatialReference origTransf2;
105  origTransf2.SetWellKnownGeogCS("WGS84");
106  poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
107  }
108  if (poCT == 0) {
109  WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed.");
110  }
111  }
112 
113  OGRFeature* poFeature;
114  poLayer->ResetReading();
115  while ((poFeature = poLayer->GetNextFeature()) != NULL) {
116  // read in edge attributes
117  std::string id = poFeature->GetFieldAsString(idField.c_str());
118  id = StringUtils::prune(id);
119  if (id == "") {
120  throw ProcessError("Missing id under '" + idField + "'");
121  }
122  id = prefix + id;
123  // read in the geometry
124  OGRGeometry* poGeometry = poFeature->GetGeometryRef();
125  if (poGeometry != 0) {
126  // try transform to wgs84
127  poGeometry->transform(poCT);
128  }
129  OGRwkbGeometryType gtype = poGeometry->getGeometryType();
130  switch (gtype) {
131  case wkbPoint: {
132  OGRPoint* cgeom = (OGRPoint*) poGeometry;
133  Position pos((SUMOReal) cgeom->getX(), (SUMOReal) cgeom->getY());
134  if (!geoConvHelper.x2cartesian(pos)) {
135  WRITE_ERROR("Unable to project coordinates for POI '" + id + "'.");
136  }
137  PointOfInterest* poi = new PointOfInterest(id, type, pos, color);
138  if (!toFill.insert(id, poi, layer)) {
139  WRITE_ERROR("POI '" + id + "' could not been added.");
140  delete poi;
141  }
142  }
143  break;
144  case wkbLineString: {
145  OGRLineString* cgeom = (OGRLineString*) poGeometry;
146  PositionVector shape;
147  for (int j = 0; j < cgeom->getNumPoints(); j++) {
148  Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
149  if (!geoConvHelper.x2cartesian(pos)) {
150  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
151  }
152  shape.push_back_noDoublePos(pos);
153  }
154  Polygon* poly = new Polygon(id, type, color, shape, false);
155  if (!toFill.insert(id, poly, layer)) {
156  WRITE_ERROR("Polygon '" + id + "' could not been added.");
157  delete poly;
158  }
159  }
160  break;
161  case wkbPolygon: {
162  OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
163  PositionVector shape;
164  for (int j = 0; j < cgeom->getNumPoints(); j++) {
165  Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j));
166  if (!geoConvHelper.x2cartesian(pos)) {
167  WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'.");
168  }
169  shape.push_back_noDoublePos(pos);
170  }
171  Polygon* poly = new Polygon(id, type, color, shape, true);
172  if (!toFill.insert(id, poly, layer)) {
173  WRITE_ERROR("Polygon '" + id + "' could not been added.");
174  delete poly;
175  }
176  }
177  break;
178  case wkbMultiPoint: {
179  OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
180  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
181  OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
182  Position pos((SUMOReal) cgeom2->getX(), (SUMOReal) cgeom2->getY());
183  std::string tid = id + "#" + toString(i);
184  if (!geoConvHelper.x2cartesian(pos)) {
185  WRITE_ERROR("Unable to project coordinates for POI '" + tid + "'.");
186  }
187  PointOfInterest* poi = new PointOfInterest(tid, type, pos, color);
188  if (!toFill.insert(tid, poi, layer)) {
189  WRITE_ERROR("POI '" + tid + "' could not been added.");
190  delete poi;
191  }
192  }
193  }
194  break;
195  case wkbMultiLineString: {
196  OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
197  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
198  OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
199  PositionVector shape;
200  std::string tid = id + "#" + toString(i);
201  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
202  Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
203  if (!geoConvHelper.x2cartesian(pos)) {
204  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
205  }
206  shape.push_back_noDoublePos(pos);
207  }
208  Polygon* poly = new Polygon(tid, type, color, shape, false);
209  if (!toFill.insert(tid, poly, layer)) {
210  WRITE_ERROR("Polygon '" + tid + "' could not been added.");
211  delete poly;
212  }
213  }
214  }
215  break;
216  case wkbMultiPolygon: {
217  OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
218  for (int i = 0; i < cgeom->getNumGeometries(); ++i) {
219  OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
220  PositionVector shape;
221  std::string tid = id + "#" + toString(i);
222  for (int j = 0; j < cgeom2->getNumPoints(); j++) {
223  Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j));
224  if (!geoConvHelper.x2cartesian(pos)) {
225  WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'.");
226  }
227  shape.push_back_noDoublePos(pos);
228  }
229  Polygon* poly = new Polygon(tid, type, color, shape, true);
230  if (!toFill.insert(tid, poly, layer)) {
231  WRITE_ERROR("Polygon '" + tid + "' could not been added.");
232  delete poly;
233  }
234  }
235  }
236  break;
237  default:
238  WRITE_WARNING("Unsupported shape type occured (id='" + id + "').");
239  break;
240  }
241  OGRFeature::DestroyFeature(poFeature);
242  }
244 #else
245  WRITE_ERROR("SUMO was compiled without GDAL support.");
246 #endif
247 }
248 
249 
250 /****************************************************************************/
251