本文整理汇总了Python中django.contrib.gis.gdal.OGRGeometry方法的典型用法代码示例。如果您正苦于以下问题:Python gdal.OGRGeometry方法的具体用法?Python gdal.OGRGeometry怎么用?Python gdal.OGRGeometry使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.gis.gdal
的用法示例。
在下文中一共展示了gdal.OGRGeometry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_python
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def to_python(self, value):
if value in self.empty_values:
return None
sref = None
# Work with a single GeoJSON geometry or a Feature.
value = json.loads(value) if '"Feature"' in value else value
if isinstance(value, collections.Mapping):
feat = sc.as_feature(value)
value = json.dumps(feat.get('geometry') or value)
sref = feat.srs
# Handle a comma delimited extent.
elif list(value).count(',') == 3:
value = Envelope(value.split(',')).polygon.ExportToWkt()
try:
geom = gdal.OGRGeometry(value, srs=getattr(sref, 'wkt', None))
except (gdal.GDALException, TypeError, ValueError):
raise forms.ValidationError(self.error_messages['invalid_geom'],
code='invalid_geom')
if not geom.srs:
geom.srid = self.srid or self.widget.map_srid
return geom
示例2: test_shapefile
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def test_shapefile(self):
base = 'dir/geofield.shp'
path = default_storage.path(base)
os.mkdir(os.path.dirname(path))
write_shp(path, _geom)
b = io.BytesIO()
with zipfile.ZipFile(b, 'w') as zf:
for ext in ('dbf', 'prj', 'shp', 'shx'):
fname = base.replace('shp', ext)
with default_storage.open(fname) as fp:
zf.writestr(fname, fp.read())
shutil.rmtree(os.path.dirname(path))
upfile = SimpleUploadedFile('geofield.zip', b.getvalue())
b.close()
result = self.field.to_python(upfile)
self.assertIsInstance(result, OGRGeometry)
self.assertIsNotNone(result.srs)
示例3: test_custom_srid
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def test_custom_srid(self):
"""Test with a null srid and a srid unknown to GDAL."""
for srid in [None, 999999]:
pnt = Point(111200, 220900, srid=srid)
self.assertTrue(pnt.ewkt.startswith(("SRID=%s;" % srid if srid else '') + "POINT (111200"))
self.assertIsInstance(pnt.ogr, gdal.OGRGeometry)
self.assertIsNone(pnt.srs)
# Test conversion from custom to a known srid
c2w = gdal.CoordTransform(
gdal.SpatialReference(
'+proj=mill +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R_A +ellps=WGS84 '
'+datum=WGS84 +units=m +no_defs'
),
gdal.SpatialReference(4326))
new_pnt = pnt.transform(c2w, clone=True)
self.assertEqual(new_pnt.srid, 4326)
self.assertAlmostEqual(new_pnt.x, 1, 3)
self.assertAlmostEqual(new_pnt.y, 2, 3)
示例4: from_gml
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def from_gml(cls, gml_string):
return gdal.OGRGeometry.from_gml(gml_string).geos
# Comparison operators
示例5: _ogr_ptr
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def _ogr_ptr(self):
return gdal.OGRGeometry._from_wkb(self.wkb)
示例6: ogr
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def ogr(self):
"Return the OGR Geometry for this Geometry."
return gdal.OGRGeometry(self._ogr_ptr(), self.srs)
示例7: convert_postgis_ewkb_to_ewkt
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def convert_postgis_ewkb_to_ewkt(ewkb_hex):
# Assert that format is little endian, capitalized, and SRID=4326
if ewkb_hex[6:18] != '0020E6100000':
raise NotWgs84EwkbValueError(ewkb_hex)
wkb_hex = ewkb_hex[0:6] + '0000' + ewkb_hex[18:]
return 'SRID=4326;' + OGRGeometry(wkb_hex).wkt
示例8: test_to_python
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def test_to_python(self):
self.assertIsInstance(self.field.to_python(self.fp), OGRGeometry)
fp = SimpleUploadedFile('empty.json', b'{}')
self.assertRaises(forms.ValidationError, self.field.to_python, fp)
示例9: test_feature_to_python
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def test_feature_to_python(self):
feature = Feature(geometry=_geom)
self.fp.write(str(feature).encode('ascii'))
self.fp.seek(0)
v = self.field.to_python(self.fp)
self.assertIsInstance(v, OGRGeometry)
示例10: ogr
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def ogr(self):
"Returns the OGR Geometry for this Geometry."
if not gdal.HAS_GDAL:
raise GEOSException('GDAL required to convert to an OGRGeometry.')
if self.srid:
try:
return gdal.OGRGeometry(self.wkb, self.srid)
except gdal.SRSException:
pass
return gdal.OGRGeometry(self.wkb)
示例11: test_gdal
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def test_gdal(self):
"Testing `ogr` and `srs` properties."
g1 = fromstr('POINT(5 23)')
self.assertIsInstance(g1.ogr, gdal.OGRGeometry)
self.assertIsNone(g1.srs)
g1_3d = fromstr('POINT(5 23 8)')
self.assertIsInstance(g1_3d.ogr, gdal.OGRGeometry)
self.assertEqual(g1_3d.ogr.z, 8)
g2 = fromstr('LINESTRING(0 0, 5 5, 23 23)', srid=4326)
self.assertIsInstance(g2.ogr, gdal.OGRGeometry)
self.assertIsInstance(g2.srs, gdal.SpatialReference)
self.assertEqual(g2.hex, g2.ogr.hex)
self.assertEqual('WGS 84', g2.srs.name)
示例12: transform
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def transform(self, ct, clone=False):
"""
Requires GDAL. Transform the geometry according to the given
transformation object, which may be an integer SRID, and WKT or
PROJ.4 string. By default, transform the geometry in-place and return
nothing. However if the `clone` keyword is set, don't modify the
geometry and return a transformed clone instead.
"""
srid = self.srid
if ct == srid:
# short-circuit where source & dest SRIDs match
if clone:
return self.clone()
else:
return
if isinstance(ct, gdal.CoordTransform):
# We don't care about SRID because CoordTransform presupposes
# source SRS.
srid = None
elif srid is None or srid < 0:
raise GEOSException("Calling transform() with no SRID set is not supported")
# Creating an OGR Geometry, which is then transformed.
g = gdal.OGRGeometry(self._ogr_ptr(), srid)
g.transform(ct)
# Getting a new GEOS pointer
ptr = g._geos_ptr()
if clone:
# User wants a cloned transformed geometry returned.
return GEOSGeometry(ptr, srid=g.srid)
if ptr:
# Reassigning pointer, and performing post-initialization setup
# again due to the reassignment.
capi.destroy_geom(self.ptr)
self.ptr = ptr
self._post_init()
self.srid = g.srid
else:
raise GEOSException('Transformed WKB was invalid.')
# #### Topology Routines ####
示例13: __init__
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def __init__(self, geo_input, srid=None):
"""
The base constructor for GEOS geometry objects. It may take the
following inputs:
* strings:
- WKT
- HEXEWKB (a PostGIS-specific canonical form)
- GeoJSON (requires GDAL)
* buffer:
- WKB
The `srid` keyword specifies the Source Reference Identifier (SRID)
number for this Geometry. If not provided, it defaults to None.
"""
input_srid = None
if isinstance(geo_input, bytes):
geo_input = force_text(geo_input)
if isinstance(geo_input, str):
wkt_m = wkt_regex.match(geo_input)
if wkt_m:
# Handle WKT input.
if wkt_m.group('srid'):
input_srid = int(wkt_m.group('srid'))
g = self._from_wkt(force_bytes(wkt_m.group('wkt')))
elif hex_regex.match(geo_input):
# Handle HEXEWKB input.
g = wkb_r().read(force_bytes(geo_input))
elif json_regex.match(geo_input):
# Handle GeoJSON input.
ogr = gdal.OGRGeometry.from_json(geo_input)
g = ogr._geos_ptr()
input_srid = ogr.srid
else:
raise ValueError('String input unrecognized as WKT EWKT, and HEXEWKB.')
elif isinstance(geo_input, GEOM_PTR):
# When the input is a pointer to a geometry (GEOM_PTR).
g = geo_input
elif isinstance(geo_input, memoryview):
# When the input is a buffer (WKB).
g = wkb_r().read(geo_input)
elif isinstance(geo_input, GEOSGeometry):
g = capi.geom_clone(geo_input.ptr)
else:
raise TypeError('Improper geometry input type: %s' % type(geo_input))
if not g:
raise GEOSException('Could not initialize GEOS Geometry with given input.')
input_srid = input_srid or capi.geos_get_srid(g) or None
if input_srid and srid and input_srid != srid:
raise ValueError('Input geometry already has SRID: %d.' % input_srid)
super().__init__(g, None)
# Set the SRID, if given.
srid = input_srid or srid
if srid and isinstance(srid, int):
self.srid = srid
示例14: __init__
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def __init__(self, geo_input, srid=None):
"""
The base constructor for GEOS geometry objects, and may take the
following inputs:
* strings:
- WKT
- HEXEWKB (a PostGIS-specific canonical form)
- GeoJSON (requires GDAL)
* buffer:
- WKB
The `srid` keyword is used to specify the Source Reference Identifier
(SRID) number for this Geometry. If not set, the SRID will be None.
"""
if isinstance(geo_input, bytes):
geo_input = force_text(geo_input)
if isinstance(geo_input, six.string_types):
wkt_m = wkt_regex.match(geo_input)
if wkt_m:
# Handling WKT input.
if wkt_m.group('srid'):
srid = int(wkt_m.group('srid'))
g = wkt_r().read(force_bytes(wkt_m.group('wkt')))
elif hex_regex.match(geo_input):
# Handling HEXEWKB input.
g = wkb_r().read(force_bytes(geo_input))
elif json_regex.match(geo_input):
# Handling GeoJSON input.
if not gdal.HAS_GDAL:
raise ValueError('Initializing geometry from JSON input requires GDAL.')
g = wkb_r().read(gdal.OGRGeometry(geo_input).wkb)
else:
raise ValueError('String or unicode input unrecognized as WKT EWKT, and HEXEWKB.')
elif isinstance(geo_input, GEOM_PTR):
# When the input is a pointer to a geometry (GEOM_PTR).
g = geo_input
elif isinstance(geo_input, six.memoryview):
# When the input is a buffer (WKB).
g = wkb_r().read(geo_input)
elif isinstance(geo_input, GEOSGeometry):
g = capi.geom_clone(geo_input.ptr)
else:
# Invalid geometry type.
raise TypeError('Improper geometry input type: %s' % str(type(geo_input)))
if g:
# Setting the pointer object with a valid pointer.
self.ptr = g
else:
raise GEOSException('Could not initialize GEOS Geometry with given input.')
# Post-initialization setup.
self._post_init(srid)
示例15: __init__
# 需要导入模块: from django.contrib.gis import gdal [as 别名]
# 或者: from django.contrib.gis.gdal import OGRGeometry [as 别名]
def __init__(self, geom, precision=None, return_GDAL=True):
"""
accepts a gdal.OGRGeometry or geos.GEOSGeometry
object and wraps multiple
VWSimplifiers. set return_GDAL to False for faster
filtering with arrays of floats returned instead of
geometry objects.
"""
global p
self.return_GDAL = return_GDAL
if isinstance(geom, OGRGeometry):
name = geom.geom_name
self.Geometry = lambda w: OGRGeometry(w, srs=geom.srs)
self.pts = np.array(geom.tuple)
elif isinstance(geom, GEOSGeometry):
name = geom.geom_type.upper()
self.Geometry = lambda w: fromstr(w)
self.pts = np.array(geom.tuple)
elif isinstance(geom, unicode) or isinstance(geom, str):
# assume wkt
# for WKT
def str2tuple(q):
return '(%s,%s)' % (q.group(1), q.group(2))
self.return_GDAL = False # don't even try
self.Geometry = lambda w: w # this will never be used
name, pts = geom.split(' ', 1)
self.pts = loads(p.sub(str2tuple, pts). \
replace('(', '[').replace(')', ']'))
self.precision = precision
if name == 'LINESTRING':
self.maskfunc = self.linemask
self.buildfunc = self.linebuild
self.fromnumfunc = self.notimplemented
elif name == "POLYGON":
self.maskfunc = self.polymask
self.buildfunc = self.polybuild
self.fromnumfunc = self.notimplemented
elif name == "MULTIPOLYGON":
self.maskfunc = self.multimask
self.buildfunc = self.multibuild
self.fromnumfunc = self.notimplemented
else:
raise RuntimeError("""
Only types LINESTRING, POLYGON and MULTIPOLYGON
supported, but got %s""" % name)
# sets self.simplifiers to a list of VWSimplifiers
self.buildfunc()
# rather than concise, I'd rather be explicit and clear.