Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, warehouse, cache, force=False):
import ogr # In the initializer b/c it is an optional dependency
super(OgrExtractor, self).__init__(warehouse, cache, force=force)
self.mangled_names = {}
# Inside the initializer because org is an optional depency
self.geo_map = {
'POLYGON': ogr.wkbPolygon,
'MULTIPOLYGON': ogr.wkbMultiPolygon,
'POINT': ogr.wkbPoint,
'MULTIPOINT': ogr.wkbMultiPoint,
# There are a lot more , add them as they are encountered.
}
self._ogr_type_map = {
None: ogr.OFTString,
'': ogr.OFTString,
'TEXT': ogr.OFTString,
'VARCHAR': ogr.OFTString,
'INT': ogr.OFTInteger,
'INTEGER': ogr.OFTInteger,
'REAL': ogr.OFTReal,
'FLOAT': ogr.OFTReal,
}
def get_geometry(self, row):
x, y = self.geo_vals(row)
if self.type == 'point':
geometry = ogr.Geometry(ogr.wkbPoint)
geometry.SetPoint_2D(0, x, y)
elif self.geo_col_names[0].lower() == 'geometry':
geometry = ogr.CreateGeometryFromWkt(x)
elif self.geo_col_names[0] == 'wkt':
geometry = ogr.CreateGeometryFromWkt(x)
elif self.geo_col_names[0] == 'wkb':
geometry = ogr.CreateGeometryFromWkb(x)
else:
raise Exception("Didn't find geometry column")
if geometry:
if not geometry.TransformTo(self.srs):
raise Exception("Failed to transform Geometry")
else:
raise Exception(
elif EQUAL(args[iArg], "-sql") and iArg < nArgc - 1:
iArg = iArg + 1
pszSQLStatement = args[iArg]
elif EQUAL(args[iArg], "-nln") and iArg < nArgc - 1:
iArg = iArg + 1
pszNewLayerName = args[iArg]
elif EQUAL(args[iArg], "-nlt") and iArg < nArgc - 1:
if EQUAL(args[iArg + 1], "NONE"):
eGType = ogr.wkbNone
elif EQUAL(args[iArg + 1], "GEOMETRY"):
eGType = ogr.wkbUnknown
elif EQUAL(args[iArg + 1], "POINT"):
eGType = ogr.wkbPoint
elif EQUAL(args[iArg + 1], "LINESTRING"):
eGType = ogr.wkbLineString
elif EQUAL(args[iArg + 1], "POLYGON"):
eGType = ogr.wkbPolygon
elif EQUAL(args[iArg + 1], "GEOMETRYCOLLECTION"):
eGType = ogr.wkbGeometryCollection
elif EQUAL(args[iArg + 1], "MULTIPOINT"):
eGType = ogr.wkbMultiPoint
elif EQUAL(args[iArg + 1], "MULTILINESTRING"):
eGType = ogr.wkbMultiLineString
elif EQUAL(args[iArg + 1], "MULTIPOLYGON"):
eGType = ogr.wkbMultiPolygon
elif EQUAL(args[iArg + 1], "GEOMETRY25D"):
eGType = ogr.wkbUnknown | ogr.wkb25DBit
elif EQUAL(args[iArg + 1], "POINT25D"):
eGType = ogr.wkbPoint25D
# Source shapefile
driver = ogr.GetDriverByName("ESRI Shapefile")
src = driver.Open(srcName, 0)
srcLyr = src.GetLayer()
# Source spatial reference
src_spatRef = srcLyr.GetSpatialRef()
# Target shapefile -
# delete if it's already
# there.
if os.path.exists(tgtName):
driver.DeleteDataSource(tgtName)
tgt = driver.CreateDataSource(tgtName)
lyrName = os.path.splitext(tgtName)[0]
tgtLyr = tgt.CreateLayer(lyrName, geom_type=ogr.wkbPoint)
# Layer definition
featDef = srcLyr.GetLayerDefn()
# Spatial Transform
trans = osr.CoordinateTransformation(src_spatRef, tgt_spatRef)
# Reproject and copy features
srcFeat = srcLyr.GetNextFeature()
while srcFeat:
geom = srcFeat.GetGeometryRef()
geom.Transform(trans)
feature = ogr.Feature(featDef)
feature.SetGeometry(geom)
tgtLyr.CreateFeature(feature)
feature.Destroy()
def SetZ (poGeom, dfZ):
if poGeom is None:
return
eGType = wkbFlatten(poGeom.GetGeometryType())
if eGType == ogr.wkbPoint:
poGeom.SetPoint(0, poGeom.GetX(), poGeom.GetY(), dfZ)
elif eGType == ogr.wkbLineString or \
eGType == ogr.wkbLinearRing:
for i in range(poGeom.GetPointCount()):
poGeom.SetPoint(i, poGeom.GetX(i), poGeom.GetY(i), dfZ)
elif eGType == ogr.wkbPolygon or \
eGType == ogr.wkbMultiPoint or \
eGType == ogr.wkbMultiLineString or \
eGType == ogr.wkbMultiPolygon or \
eGType == ogr.wkbGeometryCollection:
for i in range(poGeom.GetGeometryCount()):
SetZ(poGeom.GetGeometryRef(i), dfZ)
if gtype == ogr.wkbPolygon or gtype == ogr.wkbPolygon25D:
geom = geometry.GetGeometryRef(0)
coordinates = get_coordinates(geom)
return [coordinates]
if gtype == ogr.wkbMultiPolygon or gtype == ogr.wkbMultiPolygon25D:
coordinates = []
geom_count = geometry.GetGeometryCount()
for g in range(geom_count):
geom = geometry.GetGeometryRef(g)
coordinates.append(get_coordinates(geom))
return coordinates
types = { ogr.wkbPoint:'Point',
ogr.wkbLineString: 'LineString',
ogr.wkbPolygon: 'Polygon',
ogr.wkbMultiPoint: 'MultiPoint',
ogr.wkbMultiLineString: 'MultiLineString',
ogr.wkbMultiPolygon: 'MultiPolygon',
ogr.wkbGeometryCollection: 'GeometryCollection'
}
output = {'type': types[geometry.GetGeometryType()],
'coordinates': get_coordinates(geometry)}
return output
def create_shapefile(data_list, fileDest):
os.environ['SHAPE_ENCODING'] = "utf-8"
driver=ogr.GetDriverByName('ESRI Shapefile')
data_source = driver.CreateDataSource(fileDest)
# create the spatial reference, WGS84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# create the layer
layer = data_source.CreateLayer("pois", srs, ogr.wkbPoint)
# Add the fields we're interested in
field_name = ogr.FieldDefn("Name", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)
field_2 = ogr.FieldDefn("NameAndA", ogr.OFTString)
field_2.SetWidth(240)
layer.CreateField(field_2)
field_3 = ogr.FieldDefn("Category", ogr.OFTString)
field_3.SetWidth(50)
layer.CreateField(field_3)
field_4 = ogr.FieldDefn("Subcat", ogr.OFTString)
field_4.SetWidth(50)
layer.CreateField(field_4)
field_5 = ogr.FieldDefn("Address", ogr.OFTString)
field_5.SetWidth(140)
shp = driver.Open(shpfile, 0)
lyr = shp.GetLayer()
lyr.ResetReading()
XY=[]
field=[]
for feat in lyr:
feat_defn = lyr.GetLayerDefn()
for i in range(feat_defn.GetFieldCount()):
field_defn = feat_defn.GetFieldDefn(i)
if FIELDNAME==None:
geom = feat.GetGeometryRef()
#ztmp = float(feat.GetField(i))
if geom.GetGeometryType() == ogr.wkbPoint: # point
field.append(ztmp)
XY.append([geom.getX(),geom.getY()])
elif geom.GetGeometryType() == 2: # line
xyall=geom.GetPoints()
XY.append(np.asarray(xyall))
#field.append(ztmp)
elif geom.GetGeometryType() == 5: # multiline
for ii in range(0,geom.GetGeometryCount()):
geom2 = geom.GetGeometryRef(ii)
xyall=geom2.GetPoints()
XY.append(np.asarray(xyall))
#field.append(ztmp)
elif field_defn.GetName() == FIELDNAME:
geom = feat.GetGeometryRef()
def CreateGeometryFromJson(self, input):
try:
input['type']
except TypeError:
try:
import simplejson
except ImportError:
raise ImportError, "You must have 'simplejson' installed to be able to use this functionality"
input = simplejson.loads(input)
types = { 'Point': ogr.wkbPoint,
'LineString': ogr.wkbLineString,
'Polygon': ogr.wkbPolygon,
'MultiPoint': ogr.wkbMultiPoint,
'MultiLineString': ogr.wkbMultiLineString,
'MultiPolygon': ogr.wkbMultiPolygon,
'GeometryCollection': ogr.wkbGeometryCollection
}
type = input['type']
gtype = types[type]
geometry = ogr.Geometry(type=gtype)
coordinates = input['coordinates']
if type == 'Point':
geometry.AddPoint_2D(coordinates[0], coordinates[1])
"bytea":ogr.OFTBinary,
"character":ogr.OFTString,
"date":ogr.OFTDate,
"double precision":ogr.OFTReal,
"integer":ogr.OFTInteger,
"numeric":ogr.OFTReal,
"real":ogr.OFTReal,
"smallint":ogr.OFTInteger,
"serial":ogr.OFTInteger,
"text":ogr.OFTString,
"timestamp without time zone":ogr.OFTDateTime
}
#WKT geometry mappings
wkbTypes={
"POINT":ogr.wkbPoint,
"LINESTRING":ogr.wkbLineString,
"POLYGON":ogr.wkbPolygon,
"MULTIPOINT":ogr.wkbMultiPoint,
"MULTILINESTRING":ogr.wkbMultiLineString,
"MULTIPOLYGON":ogr.wkbMultiPolygon,
"GEOMETRYCOLLECTION":ogr.wkbGeometryCollection
}