def _freeze_geometry(self, geom): def freeze_points(geom): coords = [] for i in range(geom.GetPointCount()): coords.append([geom.GetX(i), geom.GetY(i)]) return coords geomtype = geom.GetGeometryType() & ~ogr.wkb25Bit # throw away all but the first geometry in a multigeom # sorry! if geomtype in (ogr.wkbMultiPoint, ogr.wkbMultiLineString, ogr.wkbMultiPolygon): geom = geom.GetGeometryRef(0) if geomtype not in self.freeze_type: raise ogr.OGRError( "Geometry type %d not supported by FeatureServer" % geomtype) frozen_type = self.freeze_type[geomtype] if frozen_type == "Point": points = freeze_points(geom) if len(points) == 1: coords = points[0] elif frozen_type == "LineString": coords = freeze_points(geom) elif frozen_type == "Polygon": coords = [] for i in range(geom.GetGeometryCount()): coords.append(freeze_points(geom.GetGeometryRef(i))) return {'type': frozen_type, 'coordinates': coords}
def thaw_feature(self, feature): def thaw_points(ogrgeom, coords): for coord in coords: ogrgeom.AddPoint(*coord) geom = feature.geometry if geom["type"] not in self.thaw_type: raise ogr.OGRError( "Geometry type %d not supported by FeatureServer" % geom["type"]) geomtype = self.thaw_type[geom["type"]] ogrgeom = ogr.Geometry(type=geomtype) coordinates = geom["coordinates"] if geomtype == ogr.wkbPoint: thaw_points(ogrgeom, [coordinates]) elif geomtype == ogr.wkbLineString: thaw_points(ogrgeom, coordinates) elif geomtype == ogr.wkbPolygon: for coords in coordinates: ring = ogr.Geometry(type=ogr.wkbLinearRing) thaw_points(ring, coords) ogrgeom.AddRingDirectly(ring) ogrgeom.closeRings() else: raise Exception("Unsupported geometry type") ogrfeature = ogr.Feature(self.defn) ogrfeature.SetGeometryDirectly(ogrgeom) for key, val in feature.properties.items(): key = ogrfeature.GetFieldIndex(key) ogrfeature.SetField(key, val) return ogrfeature
def update(self, action): feature = self.thaw_feature(action.feature) feature.SetFID(action.id) err = self.layer.SetFeature(feature) if err: raise ogr.OGRError("Update error on FID %d: %s" % (action.id, self.error_msgs[err])) feature.Destroy() return self.select(action)
def insert(self, action): feature = self.thaw_feature(action.feature) err = self.layer.CreateFeature(feature) if err: raise ogr.OGRError("Create error: %s" % self.error_msgs[err]) action.id = feature.GetFID() feature.Destroy() if action.id > 0: # because the OGR PostGIS driver sux return self.select(action) else: action.feature.id = action.id return [action.feature]
def delete(self, action): err = self.layer.DeleteFeature(action.id) if err: raise ogr.OGRError("Delete error on FID %d: %s" % (action.id, self.error_msgs[err])) return []