40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wpedantic"
43 #include <ogrsf_frmts.h>
45 #pragma GCC diagnostic pop
56 if (!oc.
isSet(
"shapefile-prefixes")) {
60 std::vector<std::string> files = oc.
getStringVector(
"shapefile-prefixes");
61 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
63 load(*file, oc, toFill, tm);
76 const std::string idField = oc.
getString(
"shapefile.id-column");
77 const bool useRunningID = oc.
getBool(
"shapefile.use-running-id") || idField ==
"";
79 std::string shpName = file +
".shp";
81 if (oc.
getString(
"shapefile.fill") ==
"true") {
83 }
else if (oc.
getString(
"shapefile.fill") ==
"false") {
86 #if GDAL_VERSION_MAJOR < 2
88 OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
91 GDALDataset* poDS = (GDALDataset*) GDALOpenEx(shpName.c_str(), GDAL_OF_VECTOR | GA_ReadOnly, NULL, NULL, NULL);
94 throw ProcessError(
"Could not open shape description '" + shpName +
"'.");
98 OGRLayer* poLayer = poDS->GetLayer(0);
99 poLayer->ResetReading();
102 OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
103 OGRSpatialReference destTransf;
105 destTransf.SetWellKnownGeogCS(
"WGS84");
106 OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
108 if (oc.
isSet(
"shapefile.guess-projection")) {
109 OGRSpatialReference origTransf2;
110 origTransf2.SetWellKnownGeogCS(
"WGS84");
111 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
114 WRITE_WARNING(
"Could not create geocoordinates converter; check whether proj.4 is installed.");
118 OGRFeature* poFeature;
119 poLayer->ResetReading();
121 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
122 std::vector<Parameterised*> parCont;
124 std::string
id = useRunningID ?
toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
128 throw ProcessError(
"Missing id under '" + idField +
"'");
132 for (
const std::string& typeField : oc.
getStringVector(
"shapefile.type-columns")) {
136 type += poFeature->GetFieldAsString(typeField.c_str());
139 double layer = oc.
getFloat(
"layer");
142 if (type !=
"" && tm.
has(type)) {
155 if (poFeature->GetFieldIndex(
"angle") >= 0) {
156 angle = poFeature->GetFieldAsDouble(
"angle");
159 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
160 if (poGeometry == 0) {
161 OGRFeature::DestroyFeature(poFeature);
165 poGeometry->transform(poCT);
166 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
169 OGRPoint* cgeom = (OGRPoint*) poGeometry;
170 Position pos(cgeom->getX(), cgeom->getY());
172 WRITE_ERROR(
"Unable to project coordinates for POI '" +
id +
"'.");
174 PointOfInterest* poi =
new PointOfInterest(
id, type, color, pos,
false,
"", 0, 0, layer, angle, imgFile);
175 if (toFill.
add(poi)) {
176 parCont.push_back(poi);
180 case wkbLineString: {
181 OGRLineString* cgeom = (OGRLineString*) poGeometry;
183 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
184 Position pos(cgeom->getX(j), cgeom->getY(j));
186 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
190 SUMOPolygon* poly =
new SUMOPolygon(
id, type, color, shape,
false, fillType == 1, 1, layer, angle, imgFile);
191 if (toFill.
add(poly)) {
192 parCont.push_back(poly);
197 const bool fill = fillType < 0 || fillType == 1;
198 OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
200 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
201 Position pos((
double) cgeom->getX(j), (double) cgeom->getY(j));
203 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
208 if (toFill.
add(poly)) {
209 parCont.push_back(poly);
213 case wkbMultiPoint: {
214 OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
215 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
216 OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
217 Position pos(cgeom2->getX(), cgeom2->getY());
218 std::string tid =
id +
"#" +
toString(i);
220 WRITE_ERROR(
"Unable to project coordinates for POI '" + tid +
"'.");
222 PointOfInterest* poi =
new PointOfInterest(tid, type, color, pos,
false,
"", 0, 0, layer, angle, imgFile);
223 if (toFill.
add(poi)) {
224 parCont.push_back(poi);
229 case wkbMultiLineString: {
230 OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
231 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
232 OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
234 std::string tid =
id +
"#" +
toString(i);
235 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
236 Position pos(cgeom2->getX(j), cgeom2->getY(j));
238 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
242 SUMOPolygon* poly =
new SUMOPolygon(tid, type, color, shape,
false, fillType == 1, 1, layer, angle, imgFile);
243 if (toFill.
add(poly)) {
244 parCont.push_back(poly);
249 case wkbMultiPolygon: {
250 const bool fill = fillType < 0 || fillType == 1;
251 OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
252 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
253 OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
255 std::string tid =
id +
"#" +
toString(i);
256 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
257 Position pos(cgeom2->getX(j), cgeom2->getY(j));
259 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
264 if (toFill.
add(poly)) {
265 parCont.push_back(poly);
271 WRITE_WARNING(
"Unsupported shape type occurred (id='" +
id +
"').");
274 if (oc.
getBool(
"shapefile.add-param")) {
275 for (std::vector<Parameterised*>::const_iterator it = parCont.begin(); it != parCont.end(); ++it) {
276 OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
277 for (
int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
278 OGRFieldDefn* poFieldDefn = poFDefn->GetFieldDefn(iField);
279 if (poFieldDefn->GetNameRef() != idField) {
280 if (poFieldDefn->GetType() == OFTReal) {
281 (*it)->setParameter(poFieldDefn->GetNameRef(),
toString(poFeature->GetFieldAsDouble(iField)));
289 OGRFeature::DestroyFeature(poFeature);
291 #if GDAL_VERSION_MAJOR < 2
292 OGRDataSource::DestroyDataSource(poDS);
302 WRITE_ERROR(
"SUMO was compiled without GDAL support.");