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