Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 2 get the shapefile driver
driver = ogr.GetDriverByName('ESRI Shapefile')
# 4 create a new data source and layer
fn = finalpath + '.shp'
if os.path.exists(fn):
driver.DeleteDataSource(fn)
outDS = driver.CreateDataSource(fn)
if outDS is None:
print 'Could not create consolidated shapefile'
sys.exit(1)
outLayer = outDS.CreateLayer(inputfile, geom_type=ogr.wkbPolygon)
# new field definitions for this shapefile
# dn definition
dnDefn = ogr.FieldDefn("DN", ogr.OFTInteger)
dnDefn.SetWidth(9)
dnDefn.SetPrecision(0)
outLayer.CreateField( dnDefn )
# color definition
colorDefn = ogr.FieldDefn("Color", ogr.OFTInteger)
colorDefn.SetWidth(2)
colorDefn.SetPrecision(0)
outLayer.CreateField( colorDefn )
# dot count definition
dotCountDefn = ogr.FieldDefn("DotCount", ogr.OFTInteger)
dotCountDefn.SetWidth(2)
dotCountDefn.SetPrecision(0)
outLayer.CreateField( dotCountDefn )
print '\n Convert ', infile, ' to shapefile.'
os.system('gdal_polygonize.py ' + infile + ' -f "ESRI Shapefile" ' + outfile + ' GST')
#Add glaciername
driver = ogr.GetDriverByName('ESRI Shapefile')
indataset = driver.Open(outfile, 1)
if indataset is None:
print ' Could not open file'
sys.exit(1)
inlayer = indataset.GetLayer()
fieldDefn = ogr.FieldDefn( 'glacier', ogr.OFTString)
fieldDefn.SetWidth(20)
inlayer.CreateField(fieldDefn)
fieldDefn2 = ogr.FieldDefn('quality', ogr.OFTInteger)
inlayer.CreateField(fieldDefn2)
feature = inlayer.GetNextFeature()
while feature:
feature.SetField('glacier', glaciername )
feature.SetField('quality', quality )
inlayer.SetFeature(feature)
feature.Destroy
feature = inlayer.GetNextFeature()
#close the shapefiles
indataset.Destroy()
# 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,
}
if source_type != dest_type and (self.compatible_types(source_type,dest_type) is False):
is_subset = False
break
elif len(attribute) == 10:
truncated_name = self.find_truncated_name(attribute,dest_field_schema)
if truncated_name is not None and truncated_name not in source_field_schema:
trunc_field_index = source_schema.GetFieldIndex(attribute)
truncated_fields[truncated_name] = trunc_field_index
converted_mapping[attribute] = truncated_name
if is_subset is False:
raise AttributeError('Source layer attributes are not a subset of destination.')
for truncated_name in truncated_fields:
trunc_field_index = truncated_fields[truncated_name]
name_alter = ogr.FieldDefn(truncated_name,ogr.OFTInteger)
source_layer.AlterFieldDefn(trunc_field_index,name_alter,ogr.ALTER_NAME_FLAG)
return converted_mapping
def getFieldType(fieldValue):
'Returns OGR field type appropriate for the given value'
if type(fieldValue) == float:
return ogr.OFTReal
elif type(fieldValue) == int:
return ogr.OFTInteger
elif type(fieldValue) == str:
return ogr.OFTString
# Raster geo-transform
gt = map_ds.GetGeoTransform()
# Get OSR spatial reference from raster to give to OGR dataset
map_sr = osr.SpatialReference()
map_sr.ImportFromWkt(map_ds.GetProjectionRef())
# Get OGR driver
driver = ogr.GetDriverByName(ogr_frmt)
# Create OGR dataset and layer
sample_ds = driver.CreateDataSource(output)
layer = sample_ds.CreateLayer('sample', map_sr, geom_type=ogr.wkbPolygon)
# Add fields for layer
# Sample ID field
layer.CreateField(ogr.FieldDefn('ID', ogr.OFTInteger))
# Row/Col fields
layer.CreateField(ogr.FieldDefn('ROW', ogr.OFTInteger))
layer.CreateField(ogr.FieldDefn('COL', ogr.OFTInteger))
# Strata field
layer.CreateField(ogr.FieldDefn('STRATUM', ogr.OFTInteger))
# Loop through samples adding to layer
for i, (stratum, col, row) in enumerate(zip(strata, cols, rows)):
# Feature
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetField('ID', int(i))
feature.SetField('ROW', int(row))
feature.SetField('COL', int(col))
feature.SetField('STRATUM', int(stratum))
# Geometry
# Get OSR spatial reference from raster to give to OGR dataset
map_sr = osr.SpatialReference()
map_sr.ImportFromWkt(map_ds.GetProjectionRef())
# Get OGR driver
driver = ogr.GetDriverByName(ogr_frmt)
# Create OGR dataset and layer
sample_ds = driver.CreateDataSource(output)
layer = sample_ds.CreateLayer('sample', map_sr, geom_type=ogr.wkbPolygon)
# Add fields for layer
# Sample ID field
layer.CreateField(ogr.FieldDefn('ID', ogr.OFTInteger))
# Row/Col fields
layer.CreateField(ogr.FieldDefn('ROW', ogr.OFTInteger))
layer.CreateField(ogr.FieldDefn('COL', ogr.OFTInteger))
# Strata field
layer.CreateField(ogr.FieldDefn('STRATUM', ogr.OFTInteger))
# Loop through samples adding to layer
for i, (stratum, col, row) in enumerate(zip(strata, cols, rows)):
# Feature
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetField('ID', int(i))
feature.SetField('ROW', int(row))
feature.SetField('COL', int(col))
feature.SetField('STRATUM', int(stratum))
# Geometry
ring = ogr.Geometry(type=ogr.wkbLinearRing)
for corner in corners:
outLayer.CreateField( colorDefn )
# dot count definition
dotCountDefn = ogr.FieldDefn("DotCount", ogr.OFTInteger)
dotCountDefn.SetWidth(2)
dotCountDefn.SetPrecision(0)
outLayer.CreateField( dotCountDefn )
# dot type definition
dotTypeDefn = ogr.FieldDefn("DotType", ogr.OFTInteger)
dotTypeDefn.SetWidth(1)
dotTypeDefn.SetPrecision(0)
outLayer.CreateField( dotTypeDefn )
# cross count definition
crossCountDefn = ogr.FieldDefn("CrossCount", ogr.OFTInteger)
crossCountDefn.SetWidth(2)
crossCountDefn.SetPrecision(0)
outLayer.CreateField( crossCountDefn )
# cross data definition
crossDataDefn = ogr.FieldDefn("CrossData", ogr.OFTString)
crossDataDefn.SetWidth(255)
outLayer.CreateField( crossDataDefn )
# add lat/lon as OFTReal attributes
outLayer.CreateField(ogr.FieldDefn("CentroidY", ogr.OFTReal))
outLayer.CreateField(ogr.FieldDefn("CentroidX", ogr.OFTReal))
polygonfiles = []
for files in os.listdir(path):
if files.endswith(".shp") and files.find('-polygon') != -1:
"csv":"CSV",
"tab":"MapInfo File",
"gpx":"GPX",
"dxf":"DXF"
}
ogrTypes={
"character varying":ogr.OFTString,
"bigint":ogr.OFTInteger,
"bigserial":ogr.OFTInteger,
"boolean":ogr.OFTString,
"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,
def CampiSHP(layer,feat):
feat_defn = layer.GetLayerDefn()
NumFields=feat_defn.GetFieldCount()
NameField=[]
TypeField=[]
NumFields=feat.GetFieldCount()
for i in range(NumFields):
field_defn = feat_defn.GetFieldDefn(i)
NameField.append(field_defn.GetName())
if field_defn.GetType() == ogr.OFTInteger:
TypeField.append('INTEGER')
elif field_defn.GetType() == ogr.OFTReal:
TypeField.append('REAL')
elif field_defn.GetType() == ogr.OFTString:
width=field_defn.GetWidth()
stringa='VARCHAR(%d)' % (width)
TypeField.append(stringa)
else:
TypeField.append('VARCHAR(20)')
return NameField,TypeField