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");
85 std::string shpName = file +
".shp";
87 OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
89 throw ProcessError(
"Could not open shape description '" + shpName +
"'.");
93 OGRLayer* poLayer = poDS->GetLayer(0);
94 poLayer->ResetReading();
97 OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
98 OGRSpatialReference destTransf;
100 destTransf.SetWellKnownGeogCS(
"WGS84");
101 OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
103 if (oc.
isSet(
"shapefile.guess-projection")) {
104 OGRSpatialReference origTransf2;
105 origTransf2.SetWellKnownGeogCS(
"WGS84");
106 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
109 WRITE_WARNING(
"Could not create geocoordinates converter; check whether proj.4 is installed.");
113 OGRFeature* poFeature;
114 poLayer->ResetReading();
115 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
117 std::string
id = poFeature->GetFieldAsString(idField.c_str());
120 throw ProcessError(
"Missing id under '" + idField +
"'");
124 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
125 if (poGeometry != 0) {
127 poGeometry->transform(poCT);
129 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
132 OGRPoint* cgeom = (OGRPoint*) poGeometry;
135 WRITE_ERROR(
"Unable to project coordinates for POI '" +
id +
"'.");
138 if (!toFill.
insert(
id, poi, layer)) {
139 WRITE_ERROR(
"POI '" +
id +
"' could not been added.");
144 case wkbLineString: {
145 OGRLineString* cgeom = (OGRLineString*) poGeometry;
147 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
150 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
155 if (!toFill.
insert(
id, poly, layer)) {
156 WRITE_ERROR(
"Polygon '" +
id +
"' could not been added.");
162 OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
164 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
167 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
172 if (!toFill.
insert(
id, poly, layer)) {
173 WRITE_ERROR(
"Polygon '" +
id +
"' could not been added.");
178 case wkbMultiPoint: {
179 OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
180 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
181 OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
183 std::string tid =
id +
"#" +
toString(i);
185 WRITE_ERROR(
"Unable to project coordinates for POI '" + tid +
"'.");
188 if (!toFill.
insert(tid, poi, layer)) {
189 WRITE_ERROR(
"POI '" + tid +
"' could not been added.");
195 case wkbMultiLineString: {
196 OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
197 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
198 OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
200 std::string tid =
id +
"#" +
toString(i);
201 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
204 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
209 if (!toFill.
insert(tid, poly, layer)) {
210 WRITE_ERROR(
"Polygon '" + tid +
"' could not been added.");
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();
221 std::string tid =
id +
"#" +
toString(i);
222 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
225 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
230 if (!toFill.
insert(tid, poly, layer)) {
231 WRITE_ERROR(
"Polygon '" + tid +
"' could not been added.");
238 WRITE_WARNING(
"Unsupported shape type occured (id='" +
id +
"').");
241 OGRFeature::DestroyFeature(poFeature);
245 WRITE_ERROR(
"SUMO was compiled without GDAL support.");