Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
bForceToPolygon = False
bForceToMultiPolygon = False
bForceToMultiLineString = False
poDstLayer = psInfo.poDstLayer
#papszTransformOptions = psInfo.papszTransformOptions
poCT = psInfo.poCT
panMap = psInfo.panMap
iSrcZField = psInfo.iSrcZField
if poOutputSRS is None and not bNullifyOutputSRS:
poOutputSRS = poSrcLayer.GetSpatialRef()
if wkbFlatten(eGType) == ogr.wkbPolygon:
bForceToPolygon = True
elif wkbFlatten(eGType) == ogr.wkbMultiPolygon:
bForceToMultiPolygon = True
elif wkbFlatten(eGType) == ogr.wkbMultiLineString:
bForceToMultiLineString = True
#/* -------------------------------------------------------------------- */
#/* Transfer features. */
#/* -------------------------------------------------------------------- */
nFeaturesInTransaction = 0
nCount = 0
if nGroupTransactions > 0:
poDstLayer.StartTransaction()
while True:
poDstFeature = None
"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
}
# Open input layer
ogr.UseExceptions()
poDS = ogr.Open(catchmentFeatureDBPath, OGR_UPDATE_MODE)
if not poDS:
raise Exception("Unable to open catchment feature database %s" (catchmentFeatureDBPath,))
assert(poDS.GetLayerCount() > 0)
poLayer = poDS.GetLayer(0)
assert(poLayer)
# Create output data source
poDriver = ogr.GetDriverByName(format)
assert(poDriver)
poODS = poDriver.CreateDataSource(catchmentFilepath)
assert(poODS != None)
# poOLayer = poODS.CreateLayer("catchment", poLayer.GetSpatialRef(), poLayer.GetGeomType())
poOLayer = poODS.CreateLayer("catchment", poLayer.GetSpatialRef(), ogr.wkbMultiPolygon )
# poOLayer = poODS.CreateLayer("catchment", poLayer.GetSpatialRef(), ogr.wkbPolygon )
# Create fields in output layer
layerDefn = poLayer.GetLayerDefn()
i = 0
fieldCount = layerDefn.GetFieldCount()
while i < fieldCount:
fieldDefn = layerDefn.GetFieldDefn(i)
poOLayer.CreateField(fieldDefn)
i = i + 1
# Create single geometry to hold catchment polygon in output shapefile
outGeom = ogr.Geometry( poOLayer.GetGeomType() )
# polygon = Polygon()
# Copy features, unioning them as we go
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)
#copy fields to output
inLayerDefn = inLayer.GetLayerDefn()
for i in range(0, inLayerDefn.GetFieldCount()):
fieldDefn = inLayerDefn.GetFieldDefn(i)
outLayer.CreateField(fieldDefn)
#get the output layer's feature definition, will be used for copying field values to output
outLayerDefn = outLayer.GetLayerDefn()
#loop through all input features and generalize them
for inFeature in inLayer:
gen = 0 #assume the feature should be omitted from output
geom = inFeature.GetGeometryRef() #get reference of feature geometry
if geom.GetGeometryName() == 'MULTIPOLYGON':
out_geom = ogr.Geometry(ogr.wkbMultiPolygon) #create output geometry of given type
for i in range(0, geom.GetGeometryCount()): #iterate over polygons in multipolygon
poly = ogr.Geometry(ogr.wkbPolygon) #create output polygon geometry
g = geom.GetGeometryRef(i) # input polygon can have multiple rings
for j in range(0, g.GetGeometryCount()): #iterate over rings
ring = g.GetGeometryRef(j) #access to a ring (closed polyline)
#output: gen_ring=1 indicates that some geometry is preserved after generalisation
#output: poly will receive all generalised rings in it
#input: True means that rings are allways closed
#input: first 0 means that ogrLinearRing is geometry type for creation
#input: poly is reference to geometry in which new generalised geometry of right type will be stored
#input: alg_type - generalisation (simplification+smoothing), simplification or smoothing
#input: second 0 means that this is multigeometry (polygon with multiple rings)
gen_ring,poly = Decide(ring,True,0,poly,alg_type,0) #perform generalisation
#there were some geometry preserved, store this in gen so output feature will be created
if poLyr is None:
print("Failed to identify source layer from datasource.")
poDS.Destroy()
return None
if pszWhere is not None:
poLyr.SetAttributeFilter(pszWhere)
poFeat = poLyr.GetNextFeature()
while poFeat is not None:
poSrcGeom = poFeat.GetGeometryRef()
if poSrcGeom is not None:
eType = wkbFlatten(poSrcGeom.GetGeometryType())
if poGeom is None:
poGeom = ogr.Geometry(ogr.wkbMultiPolygon)
if eType == ogr.wkbPolygon:
poGeom.AddGeometry(poSrcGeom)
elif eType == ogr.wkbMultiPolygon:
for iGeom in range(poSrcGeom.GetGeometryCount()):
poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom))
else:
print("ERROR: Geometry not of polygon type.")
if pszSQL is not None:
poDS.ReleaseResultSet(poLyr)
poDS.Destroy()
return None
poFeat = poLyr.GetNextFeature()
if poLyr is None:
print("Failed to identify source layer from datasource.")
poDS.Destroy()
return None
if pszWhere is not None:
poLyr.SetAttributeFilter(pszWhere)
poFeat = poLyr.GetNextFeature()
while poFeat is not None:
poSrcGeom = poFeat.GetGeometryRef()
if poSrcGeom is not None:
eType = wkbFlatten(poSrcGeom.GetGeometryType())
if poGeom is None:
poGeom = ogr.Geometry( ogr.wkbMultiPolygon )
if eType == ogr.wkbPolygon:
poGeom.AddGeometry( poSrcGeom )
elif eType == ogr.wkbMultiPolygon:
for iGeom in range(poSrcGeom.GetGeometryCount()):
poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom) )
else:
print("ERROR: Geometry not of polygon type." )
if pszSQL is not None:
poDS.ReleaseResultSet( poLyr )
poDS.Destroy()
return None
poFeat = poLyr.GetNextFeature()
def __init__(self, inner_path, driver_name, file_ident, format, table_metadata):
super(OGRExporter,self).__init__()
self.format = format
out_filename = os.path.join(inner_path, '%s.%s' % (file_ident, format))
out_driver = ogr.GetDriverByName(driver_name)
out_srs = osr.SpatialReference()
out_srs.ImportFromEPSG(4326)
self.out_data = out_driver.CreateDataSource(out_filename)
# See http://gis.stackexchange.com/questions/53920/ogr-createlayer-returns-typeerror
out_layer = self.out_data.CreateLayer(file_ident.encode('utf-8'), srs=out_srs, geom_type=ogr.wkbMultiPolygon)
out_layer.CreateField(ogr.FieldDefn('geoid', ogr.OFTString))
out_layer.CreateField(ogr.FieldDefn('name', ogr.OFTString))
for (table_id, table) in table_metadata.iteritems():
for column_id, column_info in table['columns'].iteritems():
estimate_col_name, error_col_name = self.make_column_names(column_id, column_info)
out_layer.CreateField(ogr.FieldDefn(estimate_col_name, ogr.OFTReal))
out_layer.CreateField(ogr.FieldDefn(error_col_name, ogr.OFTReal))
self.out_layer = out_layer
print " -- Projecting shapefile to WGS-84 and converting to JSON"
# define ogr drivers
shp_driver = ogr.GetDriverByName('ESRI Shapefile')
json_driver = ogr.GetDriverByName('GeoJSON')
# define the input layer
shp = shp_driver.Open(shp_path)
shp_lyr = shp.GetLayer()
# create the output layer
json_path = os.path.join(base_path, name + ".geojson")
if os.path.exists(json_path):
json_driver.DeleteDataSource(json_path)
json = json_driver.CreateDataSource(json_path)
json_lyr = json.CreateLayer(json_path, geom_type=ogr.wkbMultiPolygon)
json_lyr_defn = json_lyr.GetLayerDefn()
# create the CoordinateTransformation
json_ref = osr.SpatialReference()
json_ref.ImportFromEPSG(4326)
coord_trans = osr.CoordinateTransformation(
shp_lyr.GetSpatialRef(), json_ref)
# add fields to output layer
shp_lyr_defn = shp_lyr.GetLayerDefn()
for i in range(0, shp_lyr_defn.GetFieldCount()):
field_defn = shp_lyr_defn.GetFieldDefn(i)
json_lyr.CreateField(field_defn)
# loop through the input features
shp_feat = shp_lyr.GetNextFeature()
elif EQUAL(args[iArg+1],"PROMOTE_TO_MULTI"):
bPromoteToMulti = True
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
elif EQUAL(args[iArg+1],"LINESTRING25D"):
eGType = ogr.wkbLineString25D
elif EQUAL(args[iArg+1],"POLYGON25D"):
eGType = ogr.wkbPolygon25D
elif EQUAL(args[iArg+1],"GEOMETRYCOLLECTION25D"):
eGType = ogr.wkbGeometryCollection25D
elif EQUAL(args[iArg+1],"MULTIPOINT25D"):
eGType = ogr.wkbMultiPoint25D
elif EQUAL(args[iArg+1],"MULTILINESTRING25D"):
eGType = ogr.wkbMultiLineString25D
elif EQUAL(args[iArg+1],"MULTIPOLYGON25D"):
eGType = ogr.wkbMultiPolygon25D