45 #include <ogrsf_frmts.h>
48 #ifdef CHECK_MEMORY_LEAKS
50 #endif // CHECK_MEMORY_LEAKS
59 if (!oc.
isSet(
"shapefile-prefixes")) {
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) {
66 load(*file, oc, toFill, tm);
79 std::string prefix = oc.
getString(
"prefix");
82 int layer = oc.
getInt(
"layer");
83 std::string idField = oc.
getString(
"shapefile.id-column");
84 bool useRunningID = oc.
getBool(
"shapefile.use-running-id");
86 std::string shpName = file +
".shp";
88 OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
90 throw ProcessError(
"Could not open shape description '" + shpName +
"'.");
94 OGRLayer* poLayer = poDS->GetLayer(0);
95 poLayer->ResetReading();
98 OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
99 OGRSpatialReference destTransf;
101 destTransf.SetWellKnownGeogCS(
"WGS84");
102 OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
104 if (oc.
isSet(
"shapefile.guess-projection")) {
105 OGRSpatialReference origTransf2;
106 origTransf2.SetWellKnownGeogCS(
"WGS84");
107 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
110 WRITE_WARNING(
"Could not create geocoordinates converter; check whether proj.4 is installed.");
114 OGRFeature* poFeature;
115 poLayer->ResetReading();
116 unsigned int runningID = 0;
117 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
119 std::string
id = useRunningID ?
toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
123 throw ProcessError(
"Missing id under '" + idField +
"'");
127 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
128 if (poGeometry != 0) {
130 poGeometry->transform(poCT);
132 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
135 OGRPoint* cgeom = (OGRPoint*) poGeometry;
138 WRITE_ERROR(
"Unable to project coordinates for POI '" +
id +
"'.");
141 if (!toFill.
insert(
id, poi, layer)) {
142 WRITE_ERROR(
"POI '" +
id +
"' could not be added.");
147 case wkbLineString: {
148 OGRLineString* cgeom = (OGRLineString*) poGeometry;
150 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
153 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
158 if (!toFill.
insert(
id, poly, layer)) {
159 WRITE_ERROR(
"Polygon '" +
id +
"' could not be added.");
165 OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
167 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
170 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
175 if (!toFill.
insert(
id, poly, layer)) {
176 WRITE_ERROR(
"Polygon '" +
id +
"' could not be added.");
181 case wkbMultiPoint: {
182 OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
183 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
184 OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
186 std::string tid =
id +
"#" +
toString(i);
188 WRITE_ERROR(
"Unable to project coordinates for POI '" + tid +
"'.");
191 if (!toFill.
insert(tid, poi, layer)) {
192 WRITE_ERROR(
"POI '" + tid +
"' could not be added.");
198 case wkbMultiLineString: {
199 OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
200 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
201 OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
203 std::string tid =
id +
"#" +
toString(i);
204 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
207 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
212 if (!toFill.
insert(tid, poly, layer)) {
213 WRITE_ERROR(
"Polygon '" + tid +
"' could not be added.");
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();
224 std::string tid =
id +
"#" +
toString(i);
225 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
228 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
233 if (!toFill.
insert(tid, poly, layer)) {
234 WRITE_ERROR(
"Polygon '" + tid +
"' could not be added.");
241 WRITE_WARNING(
"Unsupported shape type occured (id='" +
id +
"').");
244 OGRFeature::DestroyFeature(poFeature);
248 WRITE_ERROR(
"SUMO was compiled without GDAL support.");