summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2008-11-11 17:28:43 +0000
committerJustin Bronn <jbronn@gmail.com>2008-11-11 17:28:43 +0000
commit11736980a4c3ee023ceec55cf47ef3e19818dcac (patch)
tree2ffb9d713611f0fefa3b7610bd4cb55fc4361d5f
parent1ea005729647808888aa823718305df8e58c9787 (diff)
[1.0.X] Fixed #9566 -- made GeoJSON regex more robust.
Backport of r9392 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9393 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/gis/gdal/geometries.py2
-rw-r--r--django/contrib/gis/geos/base.py2
-rw-r--r--django/contrib/gis/tests/geometries.py16
-rw-r--r--django/contrib/gis/tests/test_gdal_geom.py5
-rw-r--r--django/contrib/gis/tests/test_geos.py5
5 files changed, 24 insertions, 6 deletions
diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py
index b6d45304da..a880a75d13 100644
--- a/django/contrib/gis/gdal/geometries.py
+++ b/django/contrib/gis/gdal/geometries.py
@@ -61,7 +61,7 @@ from django.contrib.gis.gdal.prototypes.srs import clone_srs
# Regular expressions for recognizing HEXEWKB and WKT.
hex_regex = re.compile(r'^[0-9A-F]+$', re.I)
wkt_regex = re.compile(r'^(?P<type>POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)[ACEGIMLONPSRUTY\d,\.\-\(\) ]+$', re.I)
-json_regex = re.compile(r'^\{[\s\w,\-\.\"\'\:\[\]]+\}$')
+json_regex = re.compile(r'^(\s+)?\{[\s\w,\[\]\{\}\-\."\':]+\}(\s+)?$')
#### OGRGeometry Class ####
class OGRGeometry(object):
diff --git a/django/contrib/gis/geos/base.py b/django/contrib/gis/geos/base.py
index 8200d59eec..a56b19364e 100644
--- a/django/contrib/gis/geos/base.py
+++ b/django/contrib/gis/geos/base.py
@@ -21,6 +21,7 @@ from django.contrib.gis.geos.prototypes import *
# try/except since this package may be used outside GeoDjango.
try:
from django.contrib.gis.gdal import OGRGeometry, SpatialReference, GEOJSON
+ from django.contrib.gis.gdal.geometries import json_regex
HAS_GDAL = True
except:
HAS_GDAL, GEOJSON = False, False
@@ -30,7 +31,6 @@ except:
# library. Not a substitute for good web security programming practices.
hex_regex = re.compile(r'^[0-9A-F]+$', re.I)
wkt_regex = re.compile(r'^(SRID=(?P<srid>\d+);)?(?P<wkt>(POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)[ACEGIMLONPSRUTY\d,\.\-\(\) ]+)$', re.I)
-json_regex = re.compile(r'^\{.+\}$')
class GEOSGeometry(object):
"A class that, generally, encapsulates a GEOS geometry."
diff --git a/django/contrib/gis/tests/geometries.py b/django/contrib/gis/tests/geometries.py
index e9bc6f618b..950ffdb0e5 100644
--- a/django/contrib/gis/tests/geometries.py
+++ b/django/contrib/gis/tests/geometries.py
@@ -154,4 +154,20 @@ buffer_geoms = ( (TestGeom('POINT(0 0)'),
json_geoms = (TestGeom('POINT(100 0)', json='{ "type": "Point", "coordinates": [ 100.000000, 0.000000 ] }'),
TestGeom('POLYGON((0 0, -10 0, -10 -10, 0 -10, 0 0))', json='{ "type": "Polygon", "coordinates": [ [ [ 0.000000, 0.000000 ], [ -10.000000, 0.000000 ], [ -10.000000, -10.000000 ], [ 0.000000, -10.000000 ], [ 0.000000, 0.000000 ] ] ] }'),
TestGeom('MULTIPOLYGON(((102 2, 103 2, 103 3, 102 3, 102 2)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))', json='{ "type": "MultiPolygon", "coordinates": [ [ [ [ 102.000000, 2.000000 ], [ 103.000000, 2.000000 ], [ 103.000000, 3.000000 ], [ 102.000000, 3.000000 ], [ 102.000000, 2.000000 ] ] ], [ [ [ 100.000000, 0.000000 ], [ 101.000000, 0.000000 ], [ 101.000000, 1.000000 ], [ 100.000000, 1.000000 ], [ 100.000000, 0.000000 ] ], [ [ 100.200000, 0.200000 ], [ 100.800000, 0.200000 ], [ 100.800000, 0.800000 ], [ 100.200000, 0.800000 ], [ 100.200000, 0.200000 ] ] ] ] }'),
+ TestGeom('GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101.0 0.0, 102.0 1.0))',
+ json='{ "type": "GeometryCollection", "geometries": [ { "type": "Point", "coordinates": [ 100.000000, 0.000000 ] }, { "type": "LineString", "coordinates": [ [ 101.000000, 0.000000 ], [ 102.000000, 1.000000 ] ] } ] }',
+ ),
+ TestGeom('MULTILINESTRING((100.0 0.0, 101.0 1.0),(102.0 2.0, 103.0 3.0))',
+ json="""
+
+{ "type": "MultiLineString",
+ "coordinates": [
+ [ [100.0, 0.0], [101.0, 1.0] ],
+ [ [102.0, 2.0], [103.0, 3.0] ]
+ ]
+ }
+
+""",
+ not_equal=True,
+ ),
)
diff --git a/django/contrib/gis/tests/test_gdal_geom.py b/django/contrib/gis/tests/test_gdal_geom.py
index b0dab0e9e8..8f5caa9615 100644
--- a/django/contrib/gis/tests/test_gdal_geom.py
+++ b/django/contrib/gis/tests/test_gdal_geom.py
@@ -79,8 +79,9 @@ class OGRGeomTest(unittest.TestCase):
if not GEOJSON: return
for g in json_geoms:
geom = OGRGeometry(g.wkt)
- self.assertEqual(g.json, geom.json)
- self.assertEqual(g.json, geom.geojson)
+ if not hasattr(g, 'not_equal'):
+ self.assertEqual(g.json, geom.json)
+ self.assertEqual(g.json, geom.geojson)
self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
def test02_points(self):
diff --git a/django/contrib/gis/tests/test_geos.py b/django/contrib/gis/tests/test_geos.py
index 8ea450700c..6786ad04b4 100644
--- a/django/contrib/gis/tests/test_geos.py
+++ b/django/contrib/gis/tests/test_geos.py
@@ -102,8 +102,9 @@ class GEOSTest(unittest.TestCase):
if not HAS_GDAL or not GEOJSON: return
for g in json_geoms:
geom = GEOSGeometry(g.wkt)
- self.assertEqual(g.json, geom.json)
- self.assertEqual(g.json, geom.geojson)
+ if not hasattr(g, 'not_equal'):
+ self.assertEqual(g.json, geom.json)
+ self.assertEqual(g.json, geom.geojson)
self.assertEqual(GEOSGeometry(g.wkt), GEOSGeometry(geom.json))
def test01j_eq(self):