Fixed #25940 -- Added OGRGeometry.from_gml() and GEOSGeometry.from_gml().

This commit is contained in:
Sergey Fedoseev 2016-06-10 11:50:07 +05:00 committed by Tim Graham
parent 20d1cb33c2
commit 5ce660cd65
8 changed files with 44 additions and 1 deletions

View File

@ -52,6 +52,7 @@ from django.contrib.gis.gdal.prototypes import geom as capi, srs as srs_api
from django.contrib.gis.gdal.srs import CoordTransform, SpatialReference
from django.contrib.gis.geometry.regex import hex_regex, json_regex, wkt_regex
from django.utils import six
from django.utils.encoding import force_bytes
from django.utils.six.moves import range
@ -150,6 +151,10 @@ class OGRGeometry(GDALBase):
return OGRGeometry('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' % (
x0, y0, x0, y1, x1, y1, x1, y0, x0, y0))
@classmethod
def from_gml(cls, gml_string):
return cls(capi.from_gml(force_bytes(gml_string)))
# ### Geometry set-like operations ###
# g = g1 | g2
def __or__(self, other):

View File

@ -44,6 +44,7 @@ getz = pnt_func(lgdal.OGR_G_GetZ)
# Geometry creation routines.
from_wkb = geom_output(lgdal.OGR_G_CreateFromWkb, [c_char_p, c_void_p, POINTER(c_void_p), c_int], offset=-2)
from_wkt = geom_output(lgdal.OGR_G_CreateFromWkt, [POINTER(c_char_p), c_void_p, POINTER(c_void_p)], offset=-1)
from_gml = geom_output(lgdal.OGR_G_CreateFromGML, [c_char_p])
create_geom = geom_output(lgdal.OGR_G_CreateGeometry, [c_int])
clone_geom = geom_output(lgdal.OGR_G_Clone, [c_void_p])
get_geom_ref = geom_output(lgdal.OGR_G_GetGeometryRef, [c_void_p, c_int])

View File

@ -166,6 +166,10 @@ class GEOSGeometry(GEOSBase, ListMixin):
self.ptr = ptr
self._post_init(srid)
@classmethod
def from_gml(cls, gml_string):
return gdal.OGRGeometry.from_gml(gml_string).geos
# Comparison operators
def __eq__(self, other):
"""

View File

@ -448,6 +448,12 @@ coordinate transformation::
__ http://www.gdal.org/classOGRGeometry.html
.. classmethod:: from_gml(gml_string)
.. versionadded:: 1.11
Constructs an :class:`OGRGeometry` from the given GML string.
.. classmethod:: from_bbox(bbox)
Constructs a :class:`Polygon` from the given bounding-box (a 4-tuple).

View File

@ -198,6 +198,12 @@ WKB / EWKB ``buffer``
GeoJSON ``str`` or ``unicode``
======================= ======================
.. classmethod:: GEOSGeometry.from_gml(gml_string)
.. versionadded:: 1.11
Constructs a :class:`GEOSGeometry` from the given GML string.
Properties
~~~~~~~~~~

View File

@ -74,7 +74,8 @@ Minor features
:mod:`django.contrib.gis`
~~~~~~~~~~~~~~~~~~~~~~~~~
* ...
* The new :meth:`.GEOSGeometry.from_gml` and :meth:`.OGRGeometry.from_gml`
methods allow creating geometries from GML.
:mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -543,3 +543,13 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin):
OGRGeometry('POINT(0.5 0.5)').within(OGRGeometry('POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))')), True
)
self.assertIs(OGRGeometry('POINT(0 0)').within(OGRGeometry('POINT(0 1)')), False)
def test_from_gml(self):
self.assertEqual(
OGRGeometry('POINT(0 0)'),
OGRGeometry.from_gml(
'<gml:Point gml:id="p21" srsName="http://www.opengis.net/def/crs/EPSG/0/4326">'
' <gml:pos srsDimension="2">0 0</gml:pos>'
'</gml:Point>'
),
)

View File

@ -1300,6 +1300,16 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
self.assertEqual(m.group('version'), v_geos)
self.assertEqual(m.group('capi_version'), v_capi)
def test_from_gml(self):
self.assertEqual(
GEOSGeometry('POINT(0 0)'),
GEOSGeometry.from_gml(
'<gml:Point gml:id="p21" srsName="http://www.opengis.net/def/crs/EPSG/0/4326">'
' <gml:pos srsDimension="2">0 0</gml:pos>'
'</gml:Point>'
),
)
@ignore_warnings(category=RemovedInDjango20Warning)
def test_deprecated_srid_getters_setters(self):
p = Point(1, 2, srid=123)