39 #pragma GCC diagnostic push
40 #pragma GCC diagnostic ignored "-Wpedantic"
42 #include <ogrsf_frmts.h>
44 #pragma GCC diagnostic pop
55 if (!oc.
isSet(
"shapefile-prefixes")) {
59 std::vector<std::string> files = oc.
getStringVector(
"shapefile-prefixes");
60 for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
62 load(*file, oc, toFill, tm);
75 const std::string idField = oc.
getString(
"shapefile.id-column");
76 const bool useRunningID = oc.
getBool(
"shapefile.use-running-id") || idField ==
"";
78 std::string shpName = file +
".shp";
80 if (oc.
getString(
"shapefile.fill") ==
"true") {
82 }
else if (oc.
getString(
"shapefile.fill") ==
"false") {
85 #if GDAL_VERSION_MAJOR < 2
87 OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
90 GDALDataset* poDS = (GDALDataset*) GDALOpenEx(shpName.c_str(), GDAL_OF_VECTOR | GA_ReadOnly, NULL, NULL, NULL);
93 throw ProcessError(
"Could not open shape description '" + shpName +
"'.");
97 OGRLayer* poLayer = poDS->GetLayer(0);
98 poLayer->ResetReading();
101 OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
102 OGRSpatialReference destTransf;
104 destTransf.SetWellKnownGeogCS(
"WGS84");
105 OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
107 if (oc.
isSet(
"shapefile.guess-projection")) {
108 OGRSpatialReference origTransf2;
109 origTransf2.SetWellKnownGeogCS(
"WGS84");
110 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
113 WRITE_WARNING(
"Could not create geocoordinates converter; check whether proj.4 is installed.");
117 OGRFeature* poFeature;
118 poLayer->ResetReading();
120 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
121 std::vector<Parameterised*> parCont;
123 std::string
id = useRunningID ?
toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
127 throw ProcessError(
"Missing id under '" + idField +
"'");
131 for (
const std::string& typeField : oc.
getStringVector(
"shapefile.type-columns")) {
135 type += poFeature->GetFieldAsString(typeField.c_str());
138 double layer = oc.
getFloat(
"layer");
141 if (type !=
"" && tm.
has(type)) {
154 if (poFeature->GetFieldIndex(
"angle") >= 0) {
155 angle = poFeature->GetFieldAsDouble(
"angle");
158 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
159 if (poGeometry == 0) {
160 OGRFeature::DestroyFeature(poFeature);
164 poGeometry->transform(poCT);
165 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
168 OGRPoint* cgeom = (OGRPoint*) poGeometry;
169 Position pos(cgeom->getX(), cgeom->getY());
171 WRITE_ERROR(
"Unable to project coordinates for POI '" +
id +
"'.");
173 PointOfInterest* poi =
new PointOfInterest(
id, type, color, pos,
false,
"", 0, 0, layer, angle, imgFile);
174 if (toFill.
add(poi)) {
175 parCont.push_back(poi);
179 case wkbLineString: {
180 OGRLineString* cgeom = (OGRLineString*) poGeometry;
182 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
183 Position pos(cgeom->getX(j), cgeom->getY(j));
185 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
189 SUMOPolygon* poly =
new SUMOPolygon(
id, type, color, shape,
false, fillType == 1, 1, layer, angle, imgFile);
190 if (toFill.
add(poly)) {
191 parCont.push_back(poly);
196 const bool fill = fillType < 0 || fillType == 1;
197 OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
199 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
200 Position pos((
double) cgeom->getX(j), (
double) cgeom->getY(j));
202 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
207 if (toFill.
add(poly)) {
208 parCont.push_back(poly);
212 case wkbMultiPoint: {
213 OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
214 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
215 OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
216 Position pos(cgeom2->getX(), cgeom2->getY());
217 std::string tid =
id +
"#" +
toString(i);
219 WRITE_ERROR(
"Unable to project coordinates for POI '" + tid +
"'.");
221 PointOfInterest* poi =
new PointOfInterest(tid, type, color, pos,
false,
"", 0, 0, layer, angle, imgFile);
222 if (toFill.
add(poi)) {
223 parCont.push_back(poi);
228 case wkbMultiLineString: {
229 OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
230 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
231 OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
233 std::string tid =
id +
"#" +
toString(i);
234 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
235 Position pos(cgeom2->getX(j), cgeom2->getY(j));
237 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
241 SUMOPolygon* poly =
new SUMOPolygon(tid, type, color, shape,
false, fillType == 1, 1, layer, angle, imgFile);
242 if (toFill.
add(poly)) {
243 parCont.push_back(poly);
248 case wkbMultiPolygon: {
249 const bool fill = fillType < 0 || fillType == 1;
250 OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
251 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
252 OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
254 std::string tid =
id +
"#" +
toString(i);
255 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
256 Position pos(cgeom2->getX(j), cgeom2->getY(j));
258 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
263 if (toFill.
add(poly)) {
264 parCont.push_back(poly);
270 WRITE_WARNING(
"Unsupported shape type occurred (id='" +
id +
"').");
273 if (oc.
getBool(
"shapefile.add-param")) {
274 for (std::vector<Parameterised*>::const_iterator it = parCont.begin(); it != parCont.end(); ++it) {
275 OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
276 for (
int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
277 OGRFieldDefn* poFieldDefn = poFDefn->GetFieldDefn(iField);
278 if (poFieldDefn->GetNameRef() != idField) {
279 if (poFieldDefn->GetType() == OFTReal) {
280 (*it)->setParameter(poFieldDefn->GetNameRef(),
toString(poFeature->GetFieldAsDouble(iField)));
288 OGRFeature::DestroyFeature(poFeature);
290 #if GDAL_VERSION_MAJOR < 2
291 OGRDataSource::DestroyDataSource(poDS);
301 WRITE_ERROR(
"SUMO was compiled without GDAL support.");