Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Get driver
driver = ogr.GetDriverByName("ESRI Shapefile")
# Create shapeData
shp_filename = validateShapePath(shp_filename)
if os.path.exists(shp_filename):
os.remove(shp_filename)
shapeData = driver.CreateDataSource(shp_filename)
# Create spatialReference, EPSG 4326 (lonlat)
spatialReference = osr.SpatialReference()
spatialReference.ImportFromEPSG(4326)
layerName = os.path.splitext(os.path.split(shp_filename)[1])[0]
layer = shapeData.CreateLayer(layerName, spatialReference, ogr.wkbPolygon)
layerDefinition = layer.GetLayerDefn()
field_defn = ogr.FieldDefn("level", ogr.OFTReal)
layer.CreateField(field_defn)
field_defn = ogr.FieldDefn("year", ogr.OFTReal)
layer.CreateField(field_defn)
field_defn = ogr.FieldDefn("timestamp", ogr.OFTDateTime)
layer.CreateField(field_defn)
if tdim:
time = nc.variables["time"]
time_units = time.units
time_calendar = time.calendar
if time[0] < 0:
is_paleo = True
else:
is_paleo = False
cdftime = utime(time_units, time_calendar)
for k, t in enumerate(time):
fd = ogr.FieldDefn('LATDMS', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)
fd = ogr.FieldDefn('LONG', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGD', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGM', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGS', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGH', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGDMS', ogr.OFTString)
fd.SetWidth(12)
spatialRef = src_lyr.GetSpatialRef()
# from Geometry
feature = src_lyr.GetNextFeature()
geom = feature.GetGeometryRef()
spatialRef = geom.GetSpatialReference()
src_srs.ImportFromWkt(spatialRef.ExportToWkt())
# Creating destination shp
dst_ds = drv.CreateDataSource(path_output)
dst_layer = dst_ds.GetLayerByName(path_output)
dst_layer = dst_ds.CreateLayer(
path_output, geom_type=ogr.wkbPolygon, srs=src_srs)
# Add an ID and area field
idField = ogr.FieldDefn('id', ogr.OFTInteger)
areaField = ogr.FieldDefn('area', ogr.OFTReal)
dst_layer.CreateField(idField)
dst_layer.CreateField(areaField)
# Simplification of polygons
multipoly2poly(src_lyr, para, dst_layer)
# comprehensions compose arrays results in segfaults, probably because a
# copied geometry object is being released before being used.
geos = []
for i in range(bound_lyr.GetFeatureCount()):
f = bound_lyr.GetFeature(i)
g = f.geometry()
geos.append(g.Clone())
# Combine hot spots that have intersecting bounding boxes, to get larger
# areas that cover all of the adjacent intersecting smaller areas.
geos = dg.combine_envelopes(geos, use_bb=use_bb, use_distance=use_distance)
# Write out the combined bounds areas.
lyr = ogr_ds.CreateLayer('combined_bounds', aa.srs)
lyr.CreateField(ogr.FieldDefn('id', ogr.OFTInteger))
lyr.CreateField(ogr.FieldDefn('area', ogr.OFTReal))
lyr.CreateField(ogr.FieldDefn('name', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('code', ogr.OFTString))
envelopes = []
id = 1
for env in geos:
f = ogr.Feature(lyr.GetLayerDefn())
bb = dg.create_bb(env.GetEnvelope(), env.GetSpatialReference())
f.SetGeometry(bb)
f.SetField(0, id)
f.SetField(1, bb.Area())
f.SetField(2, None)
f.SetField(3, None)
id += 1
lyr.CreateFeature(f)
envelopes.append(
"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,
"GEOMETRYCOLLECTION":ogr.wkbGeometryCollection
}
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 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
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)
:param coordinate_reference_system: The CRS of the memory layer.
#############################################################################-
# Copy the SOUNDG schema, and add an ELEV field.
out_mapping = []
for fld_index in range(field_count):
src_fd = src_defn.GetFieldDefn( fld_index )
fd = ogr.FieldDefn( src_fd.GetName(), src_fd.GetType() )
fd.SetWidth( src_fd.GetWidth() )
fd.SetPrecision( src_fd.GetPrecision() )
if shp_layer.CreateField( fd ) != 0:
out_mapping.append( -1 )
else:
out_mapping.append( shp_layer.GetLayerDefn().GetFieldCount() - 1 )
fd = ogr.FieldDefn( 'ELEV', ogr.OFTReal )
fd.SetWidth( 12 )
fd.SetPrecision( 4 )
shp_layer.CreateField( fd )
#############################################################################
# Process all SOUNDG features.
feat = src_soundg.GetNextFeature()
while feat is not None:
multi_geom = feat.GetGeometryRef()
for iPnt in range(multi_geom.GetGeometryCount()):
pnt = multi_geom.GetGeometryRef( iPnt )
feat2 = ogr.Feature(feature_def=shp_layer.GetLayerDefn())
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)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGM', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGS', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGH', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('LONGDMS', ogr.OFTString)
fd.SetWidth(12)
fd.SetPrecision(7)
csv_layer.CreateField(fd)
fd = ogr.FieldDefn('DEPTHM', ogr.OFTReal)
fd.SetWidth(12)