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";
87 #if GDAL_VERSION_MAJOR < 2
89 OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE);
92 GDALDataset* poDS = (GDALDataset*) GDALOpen(shpName.c_str(), GA_ReadOnly);
95 throw ProcessError(
"Could not open shape description '" + shpName +
"'.");
99 OGRLayer* poLayer = poDS->GetLayer(0);
100 poLayer->ResetReading();
103 OGRSpatialReference* origTransf = poLayer->GetSpatialRef();
104 OGRSpatialReference destTransf;
106 destTransf.SetWellKnownGeogCS(
"WGS84");
107 OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf);
109 if (oc.
isSet(
"shapefile.guess-projection")) {
110 OGRSpatialReference origTransf2;
111 origTransf2.SetWellKnownGeogCS(
"WGS84");
112 poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf);
115 WRITE_WARNING(
"Could not create geocoordinates converter; check whether proj.4 is installed.");
119 OGRFeature* poFeature;
120 poLayer->ResetReading();
121 unsigned int runningID = 0;
122 while ((poFeature = poLayer->GetNextFeature()) != NULL) {
123 std::vector<Parameterised*> parCont;
125 std::string
id = useRunningID ?
toString(runningID) : poFeature->GetFieldAsString(idField.c_str());
129 throw ProcessError(
"Missing id under '" + idField +
"'");
133 OGRGeometry* poGeometry = poFeature->GetGeometryRef();
134 if (poGeometry == 0) {
135 OGRFeature::DestroyFeature(poFeature);
139 poGeometry->transform(poCT);
140 OGRwkbGeometryType gtype = poGeometry->getGeometryType();
143 OGRPoint* cgeom = (OGRPoint*) poGeometry;
146 WRITE_ERROR(
"Unable to project coordinates for POI '" +
id +
"'.");
149 if (toFill.
insert(
id, poi, layer)) {
150 parCont.push_back(poi);
154 case wkbLineString: {
155 OGRLineString* cgeom = (OGRLineString*) poGeometry;
157 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
160 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
165 if (toFill.
insert(
id, poly, layer)) {
166 parCont.push_back(poly);
171 OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing();
173 for (
int j = 0; j < cgeom->getNumPoints(); j++) {
176 WRITE_ERROR(
"Unable to project coordinates for polygon '" +
id +
"'.");
181 if (toFill.
insert(
id, poly, layer)) {
182 parCont.push_back(poly);
186 case wkbMultiPoint: {
187 OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry;
188 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
189 OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i);
191 std::string tid =
id +
"#" +
toString(i);
193 WRITE_ERROR(
"Unable to project coordinates for POI '" + tid +
"'.");
196 if (toFill.
insert(tid, poi, layer)) {
197 parCont.push_back(poi);
202 case wkbMultiLineString: {
203 OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry;
204 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
205 OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i);
207 std::string tid =
id +
"#" +
toString(i);
208 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
211 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
216 if (toFill.
insert(tid, poly, layer)) {
217 parCont.push_back(poly);
222 case wkbMultiPolygon: {
223 OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry;
224 for (
int i = 0; i < cgeom->getNumGeometries(); ++i) {
225 OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing();
227 std::string tid =
id +
"#" +
toString(i);
228 for (
int j = 0; j < cgeom2->getNumPoints(); j++) {
231 WRITE_ERROR(
"Unable to project coordinates for polygon '" + tid +
"'.");
236 if (toFill.
insert(tid, poly, layer)) {
237 parCont.push_back(poly);
243 WRITE_WARNING(
"Unsupported shape type occured (id='" +
id +
"').");
246 if (oc.
getBool(
"shapefile.add-param")) {
247 for (std::vector<Parameterised*>::const_iterator it = parCont.begin(); it != parCont.end(); ++it) {
248 OGRFeatureDefn* poFDefn = poLayer->GetLayerDefn();
249 for (
int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
250 OGRFieldDefn* poFieldDefn = poFDefn->GetFieldDefn(iField);
251 if (poFieldDefn->GetNameRef() != idField) {
252 if (poFieldDefn->GetType() == OFTReal) {
253 (*it)->addParameter(poFieldDefn->GetNameRef(),
toString(poFeature->GetFieldAsDouble(iField)));
261 OGRFeature::DestroyFeature(poFeature);
265 WRITE_ERROR(
"SUMO was compiled without GDAL support.");
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static RGBColor parseColor(std::string coldef)
Parses a color information.
static GeoConvHelper & getProcessing()
the coordinate transformation to use for input conversion and processing
bool x2cartesian(Position &from, bool includeInBoundary=true)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool insert(const std::string &id, Polygon *poly, int layer, bool ignorePruning=false)
Adds a polygon to the storage.
#define WRITE_WARNING(msg)
A storage for loaded polygons and pois.
static std::string latin1_to_utf8(std::string str)
Transfers from Latin 1 (ISO-8859-1) to UTF-8.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
A storage for type mappings.
static methods for processing the coordinates conversion for the current net
A point in 2D or 3D with translation and scaling methods.
static void loadIfSet(OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Loads pois/polygons assumed to be stored as shape files-files.
static void load(const std::string &file, OptionsCont &oc, PCPolyContainer &toFill, PCTypeMap &tm)
Parses pois/polys stored within the given file.
#define PROGRESS_BEGIN_MESSAGE(msg)
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
A storage for options typed value containers)
void push_back_noDoublePos(const Position &p)
#define PROGRESS_DONE_MESSAGE()
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.