Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
0: 'Square Meters',
1: 'Square Kilometres',
2: 'Square Feet',
3: 'Square Yards',
4: 'Square Miles',
5: 'Hectares',
6: 'Acres',
7: 'Square Nautical Miles',
8: 'Square Degrees',
9: 'unknown Unit'
}
# Field type converter from QGIS to OGR
# Source http://www.gdal.org/ogr__core_8h.html#a787194be
field_type_converter = {
QVariant.String: ogr.OFTString,
QVariant.Int: ogr.OFTInteger,
QVariant.Double: ogr.OFTReal,
}
@profile
def create_memory_layer(
layer_name, geometry, coordinate_reference_system=None, fields=None):
"""Create a vector memory layer.
:param layer_name: The name of the layer.
:type layer_name: str
:param geometry: The geometry of the layer.
:rtype geometry: QgsWkbTypes (note:
from C++ QgsWkbTypes::GeometryType enum)
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)
layer.CreateField(field_5)
field_6 = ogr.FieldDefn("Municipio", ogr.OFTString)
field_6.SetWidth(80)
layer.CreateField(field_6)
# Process the text file and add the attributes and features to the shapefile
for row in data_list:
# create the feature
feature = ogr.Feature(layer.GetLayerDefn())
# Set the attributes using the values from the delimited text file
fd = ogr.FieldDefn('LATD', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATM', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATS', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATH', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATDMS', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONG', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGD', ogr.OFTReal)
fd.SetWidth(12)
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
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 )
for ff in os.listdir(inputfile):
if ff.endswith(".tif"):
base_name = ff[:ff.find(".tif")]
shapefile = inputfile + "/" + base_name + "/" + base_name + "-traced.shp"
print "Processing: " + shapefile
totalfiles = totalfiles + 1
if totalpolygons == 0:
os.system("cp " + inputfile + "/" + base_name + "/" + base_name + "-traced.prj " + finalpath + ".prj")
# 3 open the input data source and get the layer
osmbbox=make_bbox(osmbuildings.GetExtent())
msbuildings.SetSpatialFilter(osmbbox)
# setup output layer
if os.path.exists(args.outfile):
shpdriver.DeleteDataSource(args.outfile)
srs=msbuildings.GetSpatialRef()
newb=shpdriver.CreateDataSource(args.outfile)
newblayer=newb.CreateLayer('buildings', srs, geom_type=ogr.wkbPolygon)
indef=msbuildings.GetLayerDefn()
field=indef.GetFieldDefn(0)
field.SetName('height')
newblayer.CreateField(field)
field_building=ogr.FieldDefn("building", ogr.OFTString)
field_building.SetWidth(24)
newblayer.CreateField(field_building)
find_new(osmbuildings,msbuildings,newblayer)
print('New:', newblayer.GetFeatureCount())
layer = ds.CreateLayer( 'gcps', srs, geom_type = ogr.wkbPoint25D )
if pixel_out == 0:
fd = ogr.FieldDefn( 'Pixel', ogr.OFTReal )
layer.CreateField( fd )
fd = ogr.FieldDefn( 'Line', ogr.OFTReal )
layer.CreateField( fd )
else:
fd = ogr.FieldDefn( 'X', ogr.OFTReal )
layer.CreateField( fd )
fd = ogr.FieldDefn( 'Y', ogr.OFTReal )
layer.CreateField( fd )
fd = ogr.FieldDefn( 'Id', ogr.OFTString )
layer.CreateField( fd )
fd = ogr.FieldDefn( 'Info', ogr.OFTString )
layer.CreateField( fd )
# ----------------------------------------------------------------------------
# Write GCPs.
# ----------------------------------------------------------------------------
for gcp in gcps:
feat = ogr.Feature( layer.GetLayerDefn() )
geom = ogr.Geometry( ogr.wkbPoint25D )
if pixel_out == 0:
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)
layer.CreateField(field_5)
field_6 = ogr.FieldDefn("Municipio", ogr.OFTString)
field_6.SetWidth(80)
fd = ogr.FieldDefn('LATD', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATM', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATS', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATH', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LATDMS', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONG', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGD', ogr.OFTReal)
fd.SetWidth(12)
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