2017-01-03 22:56:50 +08:00
|
|
|
import re
|
|
|
|
|
2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis import forms
|
2018-02-06 18:51:41 +08:00
|
|
|
from django.contrib.gis.forms import BaseGeometryWidget, OpenLayersWidget
|
2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis.geos import GEOSGeometry
|
2020-02-12 21:48:49 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-05-04 22:14:35 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2022-08-12 18:18:51 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango51Warning
|
2013-08-30 16:48:36 +08:00
|
|
|
from django.utils.html import escape
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2013-07-01 20:22:27 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
class GeometryFieldTest(SimpleTestCase):
|
|
|
|
def test_init(self):
|
2009-04-26 02:24:32 +08:00
|
|
|
"Testing GeometryField initialization with defaults."
|
|
|
|
fld = forms.GeometryField()
|
|
|
|
for bad_default in ("blah", 3, "FoO", None, 0):
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(bad_default=bad_default):
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
fld.clean(bad_default)
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
def test_srid(self):
|
2009-04-26 02:24:32 +08:00
|
|
|
"Testing GeometryField with a SRID set."
|
|
|
|
# Input that doesn't specify the SRID is assumed to be in the SRID
|
|
|
|
# of the input field.
|
|
|
|
fld = forms.GeometryField(srid=4326)
|
|
|
|
geom = fld.clean("POINT(5 23)")
|
|
|
|
self.assertEqual(4326, geom.srid)
|
|
|
|
# Making the field in a different SRID from that of the geometry, and
|
|
|
|
# asserting it transforms.
|
|
|
|
fld = forms.GeometryField(srid=32140)
|
2021-03-23 16:16:33 +08:00
|
|
|
# Different PROJ versions use different transformations, all are
|
|
|
|
# correct as having a 1 meter accuracy.
|
|
|
|
tol = 1
|
2009-04-26 02:24:32 +08:00
|
|
|
xform_geom = GEOSGeometry(
|
|
|
|
"POINT (951640.547328465 4219369.26171664)", srid=32140
|
|
|
|
)
|
2018-02-06 18:51:41 +08:00
|
|
|
# The cleaned geometry is transformed to 32140 (the widget map_srid is 3857).
|
|
|
|
cleaned_geom = fld.clean(
|
|
|
|
"SRID=3857;POINT (-10615777.40976205 3473169.895707852)"
|
|
|
|
)
|
|
|
|
self.assertEqual(cleaned_geom.srid, 32140)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertTrue(xform_geom.equals_exact(cleaned_geom, tol))
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
def test_null(self):
|
2009-04-26 02:24:32 +08:00
|
|
|
"Testing GeometryField's handling of null (None) geometries."
|
|
|
|
# Form fields, by default, are required (`required=True`)
|
|
|
|
fld = forms.GeometryField()
|
2020-02-12 21:48:49 +08:00
|
|
|
with self.assertRaisesMessage(ValidationError, "No geometry value provided."):
|
2013-03-16 04:45:33 +08:00
|
|
|
fld.clean(None)
|
2009-04-26 02:24:32 +08:00
|
|
|
|
|
|
|
# This will clean None as a geometry (See #10660).
|
|
|
|
fld = forms.GeometryField(required=False)
|
2013-03-16 04:45:33 +08:00
|
|
|
self.assertIsNone(fld.clean(None))
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
def test_geom_type(self):
|
2009-04-26 02:24:32 +08:00
|
|
|
"Testing GeometryField's handling of different geometry types."
|
|
|
|
# By default, all geometry types are allowed.
|
|
|
|
fld = forms.GeometryField()
|
|
|
|
for wkt in (
|
|
|
|
"POINT(5 23)",
|
|
|
|
"MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))",
|
|
|
|
"LINESTRING(0 0, 1 1)",
|
|
|
|
):
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(wkt=wkt):
|
|
|
|
# to_python() uses the SRID of OpenLayersWidget if the
|
|
|
|
# converted value doesn't have an SRID.
|
|
|
|
self.assertEqual(
|
|
|
|
GEOSGeometry(wkt, srid=fld.widget.map_srid), fld.clean(wkt)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2009-04-26 02:24:32 +08:00
|
|
|
|
|
|
|
pnt_fld = forms.GeometryField(geom_type="POINT")
|
2016-11-23 16:23:06 +08:00
|
|
|
self.assertEqual(
|
|
|
|
GEOSGeometry("POINT(5 23)", srid=pnt_fld.widget.map_srid),
|
|
|
|
pnt_fld.clean("POINT(5 23)"),
|
|
|
|
)
|
2012-06-19 20:55:40 +08:00
|
|
|
# a WKT for any other geom_type will be properly transformed by `to_python`
|
2016-11-23 16:23:06 +08:00
|
|
|
self.assertEqual(
|
|
|
|
GEOSGeometry("LINESTRING(0 0, 1 1)", srid=pnt_fld.widget.map_srid),
|
|
|
|
pnt_fld.to_python("LINESTRING(0 0, 1 1)"),
|
|
|
|
)
|
2012-06-19 20:55:40 +08:00
|
|
|
# but rejected by `clean`
|
2020-02-12 21:48:49 +08:00
|
|
|
with self.assertRaises(ValidationError):
|
2016-01-17 19:26:39 +08:00
|
|
|
pnt_fld.clean("LINESTRING(0 0, 1 1)")
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
def test_to_python(self):
|
2012-06-19 20:55:40 +08:00
|
|
|
"""
|
2018-11-22 18:21:58 +08:00
|
|
|
to_python() either returns a correct GEOSGeometry object or
|
|
|
|
a ValidationError.
|
2012-06-19 20:55:40 +08:00
|
|
|
"""
|
2018-11-22 18:21:58 +08:00
|
|
|
good_inputs = [
|
|
|
|
"POINT(5 23)",
|
|
|
|
"MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))",
|
|
|
|
"LINESTRING(0 0, 1 1)",
|
|
|
|
]
|
|
|
|
bad_inputs = [
|
|
|
|
"POINT(5)",
|
|
|
|
"MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))",
|
|
|
|
"BLAH(0 0, 1 1)",
|
|
|
|
'{"type": "FeatureCollection", "features": ['
|
|
|
|
'{"geometry": {"type": "Point", "coordinates": [508375, 148905]}, '
|
|
|
|
'"type": "Feature"}]}',
|
|
|
|
]
|
2012-06-19 20:55:40 +08:00
|
|
|
fld = forms.GeometryField()
|
|
|
|
# to_python returns the same GEOSGeometry for a WKT
|
2018-11-22 18:21:58 +08:00
|
|
|
for geo_input in good_inputs:
|
|
|
|
with self.subTest(geo_input=geo_input):
|
|
|
|
self.assertEqual(
|
|
|
|
GEOSGeometry(geo_input, srid=fld.widget.map_srid),
|
|
|
|
fld.to_python(geo_input),
|
|
|
|
)
|
2012-06-19 20:55:40 +08:00
|
|
|
# but raises a ValidationError for any other string
|
2018-11-22 18:21:58 +08:00
|
|
|
for geo_input in bad_inputs:
|
|
|
|
with self.subTest(geo_input=geo_input):
|
2020-02-12 21:48:49 +08:00
|
|
|
with self.assertRaises(ValidationError):
|
2018-11-22 18:21:58 +08:00
|
|
|
fld.to_python(geo_input)
|
2012-06-19 20:55:40 +08:00
|
|
|
|
2018-02-06 18:51:41 +08:00
|
|
|
def test_to_python_different_map_srid(self):
|
|
|
|
f = forms.GeometryField(widget=OpenLayersWidget)
|
|
|
|
json = '{ "type": "Point", "coordinates": [ 5.0, 23.0 ] }'
|
|
|
|
self.assertEqual(
|
|
|
|
GEOSGeometry("POINT(5 23)", srid=f.widget.map_srid), f.to_python(json)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2018-02-06 18:51:41 +08:00
|
|
|
|
2013-11-23 17:53:09 +08:00
|
|
|
def test_field_with_text_widget(self):
|
|
|
|
class PointForm(forms.Form):
|
|
|
|
pt = forms.PointField(srid=4326, widget=forms.TextInput)
|
|
|
|
|
|
|
|
form = PointForm()
|
|
|
|
cleaned_pt = form.fields["pt"].clean("POINT(5 23)")
|
2016-11-23 16:23:06 +08:00
|
|
|
self.assertEqual(cleaned_pt, GEOSGeometry("POINT(5 23)", srid=4326))
|
2013-11-23 17:53:09 +08:00
|
|
|
self.assertEqual(4326, cleaned_pt.srid)
|
2018-02-06 18:51:41 +08:00
|
|
|
with self.assertRaisesMessage(ValidationError, "Invalid geometry value."):
|
|
|
|
form.fields["pt"].clean("POINT(5)")
|
2013-11-23 17:53:09 +08:00
|
|
|
|
|
|
|
point = GEOSGeometry("SRID=4326;POINT(5 23)")
|
|
|
|
form = PointForm(data={"pt": "POINT(5 23)"}, initial={"pt": point})
|
|
|
|
self.assertFalse(form.has_changed())
|
|
|
|
|
2016-08-06 17:14:41 +08:00
|
|
|
def test_field_string_value(self):
|
|
|
|
"""
|
|
|
|
Initialization of a geometry field with a valid/empty/invalid string.
|
|
|
|
Only the invalid string should trigger an error log entry.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2016-08-06 17:14:41 +08:00
|
|
|
class PointForm(forms.Form):
|
|
|
|
pt1 = forms.PointField(srid=4326)
|
|
|
|
pt2 = forms.PointField(srid=4326)
|
|
|
|
pt3 = forms.PointField(srid=4326)
|
|
|
|
|
|
|
|
form = PointForm(
|
|
|
|
{
|
|
|
|
"pt1": "SRID=4326;POINT(7.3 44)", # valid
|
|
|
|
"pt2": "", # empty
|
|
|
|
"pt3": "PNT(0)", # invalid
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-04-28 21:20:27 +08:00
|
|
|
with self.assertLogs("django.contrib.gis", "ERROR") as logger_calls:
|
2016-08-06 17:14:41 +08:00
|
|
|
output = str(form)
|
|
|
|
|
2017-01-03 22:56:50 +08:00
|
|
|
# The first point can't use assertInHTML() due to non-deterministic
|
|
|
|
# ordering of the rendered dictionary.
|
2020-05-11 04:03:39 +08:00
|
|
|
pt1_serialized = re.search(r"<textarea [^>]*>({[^<]+})<", output)[1]
|
2017-03-21 21:04:41 +08:00
|
|
|
pt1_json = pt1_serialized.replace(""", '"')
|
|
|
|
pt1_expected = GEOSGeometry(form.data["pt1"]).transform(3857, clone=True)
|
|
|
|
self.assertJSONEqual(pt1_json, pt1_expected.json)
|
2017-01-03 22:56:50 +08:00
|
|
|
|
2016-08-06 17:14:41 +08:00
|
|
|
self.assertInHTML(
|
|
|
|
'<textarea id="id_pt2" class="vSerializedField required" cols="150"'
|
2022-07-31 05:52:17 +08:00
|
|
|
' rows="10" name="pt2" hidden></textarea>',
|
2016-08-06 17:14:41 +08:00
|
|
|
output,
|
|
|
|
)
|
|
|
|
self.assertInHTML(
|
|
|
|
'<textarea id="id_pt3" class="vSerializedField required" cols="150"'
|
2022-07-31 05:52:17 +08:00
|
|
|
' rows="10" name="pt3" hidden></textarea>',
|
2016-08-06 17:14:41 +08:00
|
|
|
output,
|
|
|
|
)
|
2018-02-06 18:51:41 +08:00
|
|
|
# Only the invalid PNT(0) triggers an error log entry.
|
|
|
|
# Deserialization is called in form clean and in widget rendering.
|
2018-04-28 21:20:27 +08:00
|
|
|
self.assertEqual(len(logger_calls.records), 2)
|
2016-08-06 17:14:41 +08:00
|
|
|
self.assertEqual(
|
2018-04-28 21:20:27 +08:00
|
|
|
logger_calls.records[0].getMessage(),
|
2016-12-29 23:27:49 +08:00
|
|
|
"Error creating geometry from value 'PNT(0)' (String input "
|
2016-08-06 17:14:41 +08:00
|
|
|
"unrecognized as WKT EWKT, and HEXEWKB.)",
|
|
|
|
)
|
|
|
|
|
2012-06-19 20:55:40 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
class SpecializedFieldTest(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.geometries = {
|
|
|
|
"point": GEOSGeometry("SRID=4326;POINT(9.052734375 42.451171875)"),
|
|
|
|
"multipoint": GEOSGeometry(
|
|
|
|
"SRID=4326;MULTIPOINT("
|
|
|
|
"(13.18634033203125 14.504356384277344),"
|
|
|
|
"(13.207969665527 14.490966796875),"
|
|
|
|
"(13.177070617675 14.454917907714))"
|
|
|
|
),
|
|
|
|
"linestring": GEOSGeometry(
|
|
|
|
"SRID=4326;LINESTRING("
|
|
|
|
"-8.26171875 -0.52734375,"
|
|
|
|
"-7.734375 4.21875,"
|
|
|
|
"6.85546875 3.779296875,"
|
|
|
|
"5.44921875 -3.515625)"
|
|
|
|
),
|
|
|
|
"multilinestring": GEOSGeometry(
|
|
|
|
"SRID=4326;MULTILINESTRING("
|
|
|
|
"(-16.435546875 -2.98828125,"
|
|
|
|
"-17.2265625 2.98828125,"
|
|
|
|
"-0.703125 3.515625,"
|
|
|
|
"-1.494140625 -3.33984375),"
|
|
|
|
"(-8.0859375 -5.9765625,"
|
|
|
|
"8.525390625 -8.7890625,"
|
|
|
|
"12.392578125 -0.87890625,"
|
|
|
|
"10.01953125 7.646484375))"
|
|
|
|
),
|
|
|
|
"polygon": GEOSGeometry(
|
|
|
|
"SRID=4326;POLYGON("
|
|
|
|
"(-1.669921875 6.240234375,"
|
|
|
|
"-3.8671875 -0.615234375,"
|
|
|
|
"5.9765625 -3.955078125,"
|
|
|
|
"18.193359375 3.955078125,"
|
|
|
|
"9.84375 9.4921875,"
|
|
|
|
"-1.669921875 6.240234375))"
|
|
|
|
),
|
|
|
|
"multipolygon": GEOSGeometry(
|
|
|
|
"SRID=4326;MULTIPOLYGON("
|
|
|
|
"((-17.578125 13.095703125,"
|
|
|
|
"-17.2265625 10.8984375,"
|
|
|
|
"-13.974609375 10.1953125,"
|
|
|
|
"-13.359375 12.744140625,"
|
|
|
|
"-15.732421875 13.7109375,"
|
|
|
|
"-17.578125 13.095703125)),"
|
|
|
|
"((-8.525390625 5.537109375,"
|
|
|
|
"-8.876953125 2.548828125,"
|
|
|
|
"-5.888671875 1.93359375,"
|
|
|
|
"-5.09765625 4.21875,"
|
|
|
|
"-6.064453125 6.240234375,"
|
|
|
|
"-8.525390625 5.537109375)))"
|
|
|
|
),
|
|
|
|
"geometrycollection": GEOSGeometry(
|
|
|
|
"SRID=4326;GEOMETRYCOLLECTION("
|
|
|
|
"POINT(5.625 -0.263671875),"
|
|
|
|
"POINT(6.767578125 -3.603515625),"
|
|
|
|
"POINT(8.525390625 0.087890625),"
|
|
|
|
"POINT(8.0859375 -2.13134765625),"
|
|
|
|
"LINESTRING("
|
|
|
|
"6.273193359375 -1.175537109375,"
|
|
|
|
"5.77880859375 -1.812744140625,"
|
|
|
|
"7.27294921875 -2.230224609375,"
|
|
|
|
"7.657470703125 -1.25244140625))"
|
|
|
|
),
|
|
|
|
}
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
def assertMapWidget(self, form_instance):
|
|
|
|
"""
|
|
|
|
Make sure the MapWidget js is passed in the form media and a MapWidget
|
|
|
|
is actually created
|
|
|
|
"""
|
|
|
|
self.assertTrue(form_instance.is_valid())
|
|
|
|
rendered = form_instance.as_p()
|
|
|
|
self.assertIn("new MapWidget(options);", rendered)
|
2017-03-17 21:57:42 +08:00
|
|
|
self.assertIn("map_srid: 3857,", rendered)
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertIn("gis/js/OLMapWidget.js", str(form_instance.media))
|
|
|
|
|
|
|
|
def assertTextarea(self, geom, rendered):
|
|
|
|
"""Makes sure the wkt and a textarea are in the content"""
|
2013-07-01 20:22:27 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertIn("<textarea ", rendered)
|
|
|
|
self.assertIn("required", rendered)
|
2017-03-17 21:57:42 +08:00
|
|
|
ogr = geom.ogr
|
|
|
|
ogr.transform(3857)
|
|
|
|
self.assertIn(escape(ogr.json), rendered)
|
2013-03-16 18:39:18 +08:00
|
|
|
|
2019-09-11 18:55:17 +08:00
|
|
|
# map_srid in openlayers.html template must not be localized.
|
2021-09-09 13:42:05 +08:00
|
|
|
@override_settings(USE_THOUSAND_SEPARATOR=True)
|
2013-03-16 18:39:18 +08:00
|
|
|
def test_pointfield(self):
|
|
|
|
class PointForm(forms.Form):
|
|
|
|
p = forms.PointField()
|
|
|
|
|
|
|
|
geom = self.geometries["point"]
|
|
|
|
form = PointForm(data={"p": geom})
|
|
|
|
self.assertTextarea(geom, form.as_p())
|
|
|
|
self.assertMapWidget(form)
|
|
|
|
self.assertFalse(PointForm().is_valid())
|
|
|
|
invalid = PointForm(data={"p": "some invalid geom"})
|
|
|
|
self.assertFalse(invalid.is_valid())
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn("Invalid geometry value", str(invalid.errors))
|
2013-03-16 18:39:18 +08:00
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
for invalid in [geo for key, geo in self.geometries.items() if key != "point"]:
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertFalse(PointForm(data={"p": invalid.wkt}).is_valid())
|
|
|
|
|
|
|
|
def test_multipointfield(self):
|
|
|
|
class PointForm(forms.Form):
|
|
|
|
p = forms.MultiPointField()
|
|
|
|
|
|
|
|
geom = self.geometries["multipoint"]
|
|
|
|
form = PointForm(data={"p": geom})
|
|
|
|
self.assertTextarea(geom, form.as_p())
|
|
|
|
self.assertMapWidget(form)
|
|
|
|
self.assertFalse(PointForm().is_valid())
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
for invalid in [
|
|
|
|
geo for key, geo in self.geometries.items() if key != "multipoint"
|
|
|
|
]:
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertFalse(PointForm(data={"p": invalid.wkt}).is_valid())
|
|
|
|
|
|
|
|
def test_linestringfield(self):
|
|
|
|
class LineStringForm(forms.Form):
|
2016-11-15 06:40:28 +08:00
|
|
|
f = forms.LineStringField()
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
geom = self.geometries["linestring"]
|
2016-11-15 06:40:28 +08:00
|
|
|
form = LineStringForm(data={"f": geom})
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertTextarea(geom, form.as_p())
|
|
|
|
self.assertMapWidget(form)
|
|
|
|
self.assertFalse(LineStringForm().is_valid())
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
for invalid in [
|
|
|
|
geo for key, geo in self.geometries.items() if key != "linestring"
|
|
|
|
]:
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertFalse(LineStringForm(data={"p": invalid.wkt}).is_valid())
|
|
|
|
|
|
|
|
def test_multilinestringfield(self):
|
|
|
|
class LineStringForm(forms.Form):
|
2016-11-15 06:40:28 +08:00
|
|
|
f = forms.MultiLineStringField()
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
geom = self.geometries["multilinestring"]
|
2016-11-15 06:40:28 +08:00
|
|
|
form = LineStringForm(data={"f": geom})
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertTextarea(geom, form.as_p())
|
|
|
|
self.assertMapWidget(form)
|
|
|
|
self.assertFalse(LineStringForm().is_valid())
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
for invalid in [
|
|
|
|
geo for key, geo in self.geometries.items() if key != "multilinestring"
|
|
|
|
]:
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertFalse(LineStringForm(data={"p": invalid.wkt}).is_valid())
|
|
|
|
|
|
|
|
def test_polygonfield(self):
|
|
|
|
class PolygonForm(forms.Form):
|
|
|
|
p = forms.PolygonField()
|
|
|
|
|
|
|
|
geom = self.geometries["polygon"]
|
|
|
|
form = PolygonForm(data={"p": geom})
|
|
|
|
self.assertTextarea(geom, form.as_p())
|
|
|
|
self.assertMapWidget(form)
|
|
|
|
self.assertFalse(PolygonForm().is_valid())
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
for invalid in [
|
|
|
|
geo for key, geo in self.geometries.items() if key != "polygon"
|
|
|
|
]:
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertFalse(PolygonForm(data={"p": invalid.wkt}).is_valid())
|
|
|
|
|
|
|
|
def test_multipolygonfield(self):
|
|
|
|
class PolygonForm(forms.Form):
|
|
|
|
p = forms.MultiPolygonField()
|
|
|
|
|
|
|
|
geom = self.geometries["multipolygon"]
|
|
|
|
form = PolygonForm(data={"p": geom})
|
|
|
|
self.assertTextarea(geom, form.as_p())
|
|
|
|
self.assertMapWidget(form)
|
|
|
|
self.assertFalse(PolygonForm().is_valid())
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
for invalid in [
|
|
|
|
geo for key, geo in self.geometries.items() if key != "multipolygon"
|
|
|
|
]:
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertFalse(PolygonForm(data={"p": invalid.wkt}).is_valid())
|
|
|
|
|
|
|
|
def test_geometrycollectionfield(self):
|
|
|
|
class GeometryForm(forms.Form):
|
|
|
|
g = forms.GeometryCollectionField()
|
|
|
|
|
|
|
|
geom = self.geometries["geometrycollection"]
|
|
|
|
form = GeometryForm(data={"g": geom})
|
|
|
|
self.assertTextarea(geom, form.as_p())
|
|
|
|
self.assertMapWidget(form)
|
|
|
|
self.assertFalse(GeometryForm().is_valid())
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
for invalid in [
|
|
|
|
geo for key, geo in self.geometries.items() if key != "geometrycollection"
|
|
|
|
]:
|
2013-03-16 18:39:18 +08:00
|
|
|
self.assertFalse(GeometryForm(data={"g": invalid.wkt}).is_valid())
|
2013-08-30 16:48:36 +08:00
|
|
|
|
2013-12-13 22:05:45 +08:00
|
|
|
|
|
|
|
class OSMWidgetTest(SimpleTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.geometries = {
|
|
|
|
"point": GEOSGeometry("SRID=4326;POINT(9.052734375 42.451171875)"),
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:05:01 +08:00
|
|
|
def test_osm_widget(self):
|
|
|
|
class PointForm(forms.Form):
|
|
|
|
p = forms.PointField(widget=forms.OSMWidget)
|
|
|
|
|
|
|
|
geom = self.geometries["point"]
|
|
|
|
form = PointForm(data={"p": geom})
|
2013-09-03 16:01:13 +08:00
|
|
|
rendered = form.as_p()
|
2013-12-13 22:05:45 +08:00
|
|
|
|
2015-05-25 23:31:26 +08:00
|
|
|
self.assertIn("ol.source.OSM()", rendered)
|
2013-09-03 16:01:13 +08:00
|
|
|
self.assertIn("id: 'id_p',", rendered)
|
2013-09-03 15:05:01 +08:00
|
|
|
|
2013-12-13 22:05:45 +08:00
|
|
|
def test_default_lat_lon(self):
|
2017-05-12 21:47:40 +08:00
|
|
|
self.assertEqual(forms.OSMWidget.default_lon, 5)
|
|
|
|
self.assertEqual(forms.OSMWidget.default_lat, 47)
|
2017-05-15 02:31:17 +08:00
|
|
|
self.assertEqual(forms.OSMWidget.default_zoom, 12)
|
2017-05-12 21:47:40 +08:00
|
|
|
|
2013-12-13 22:05:45 +08:00
|
|
|
class PointForm(forms.Form):
|
|
|
|
p = forms.PointField(
|
|
|
|
widget=forms.OSMWidget(
|
|
|
|
attrs={
|
2017-05-15 02:31:17 +08:00
|
|
|
"default_lon": 20,
|
|
|
|
"default_lat": 30,
|
|
|
|
"default_zoom": 17,
|
2013-12-13 22:05:45 +08:00
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
form = PointForm()
|
|
|
|
rendered = form.as_p()
|
|
|
|
|
|
|
|
self.assertIn("options['default_lon'] = 20;", rendered)
|
|
|
|
self.assertIn("options['default_lat'] = 30;", rendered)
|
2017-05-15 02:31:17 +08:00
|
|
|
self.assertIn("options['default_zoom'] = 17;", rendered)
|
2013-12-13 22:05:45 +08:00
|
|
|
|
2013-09-03 15:05:01 +08:00
|
|
|
|
2017-04-20 23:36:40 +08:00
|
|
|
class GeometryWidgetTests(SimpleTestCase):
|
2017-04-29 21:35:46 +08:00
|
|
|
def test_get_context_attrs(self):
|
2020-11-28 15:58:40 +08:00
|
|
|
# The Widget.get_context() attrs argument overrides self.attrs.
|
2017-04-29 21:35:46 +08:00
|
|
|
widget = BaseGeometryWidget(attrs={"geom_type": "POINT"})
|
|
|
|
context = widget.get_context("point", None, attrs={"geom_type": "POINT2"})
|
|
|
|
self.assertEqual(context["geom_type"], "POINT2")
|
2020-11-28 15:58:40 +08:00
|
|
|
# Widget.get_context() returns expected name for geom_type.
|
|
|
|
widget = BaseGeometryWidget(attrs={"geom_type": "POLYGON"})
|
|
|
|
context = widget.get_context("polygon", None, None)
|
|
|
|
self.assertEqual(context["geom_type"], "Polygon")
|
2020-11-26 06:36:35 +08:00
|
|
|
# Widget.get_context() returns 'Geometry' instead of 'Unknown'.
|
|
|
|
widget = BaseGeometryWidget(attrs={"geom_type": "GEOMETRY"})
|
|
|
|
context = widget.get_context("geometry", None, None)
|
|
|
|
self.assertEqual(context["geom_type"], "Geometry")
|
2017-04-29 21:35:46 +08:00
|
|
|
|
2017-04-20 23:36:40 +08:00
|
|
|
def test_subwidgets(self):
|
|
|
|
widget = forms.BaseGeometryWidget()
|
|
|
|
self.assertEqual(
|
|
|
|
list(widget.subwidgets("name", "value")),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"is_hidden": False,
|
|
|
|
"attrs": {
|
|
|
|
"map_srid": 4326,
|
|
|
|
"map_width": 600,
|
|
|
|
"geom_type": "GEOMETRY",
|
|
|
|
"map_height": 400,
|
|
|
|
"display_raw": False,
|
|
|
|
},
|
|
|
|
"name": "name",
|
|
|
|
"template_name": "",
|
|
|
|
"value": "value",
|
|
|
|
"required": False,
|
|
|
|
}
|
2022-02-04 03:24:19 +08:00
|
|
|
],
|
2017-04-20 23:36:40 +08:00
|
|
|
)
|
2013-08-30 16:48:36 +08:00
|
|
|
|
|
|
|
def test_custom_serialization_widget(self):
|
2013-09-02 20:15:04 +08:00
|
|
|
class CustomGeometryWidget(forms.BaseGeometryWidget):
|
|
|
|
template_name = "gis/openlayers.html"
|
|
|
|
deserialize_called = 0
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-09-02 20:15:04 +08:00
|
|
|
def serialize(self, value):
|
|
|
|
return value.json if value else ""
|
|
|
|
|
|
|
|
def deserialize(self, value):
|
|
|
|
self.deserialize_called += 1
|
|
|
|
return GEOSGeometry(value)
|
|
|
|
|
2013-08-30 16:48:36 +08:00
|
|
|
class PointForm(forms.Form):
|
2013-09-02 20:15:04 +08:00
|
|
|
p = forms.PointField(widget=CustomGeometryWidget)
|
2013-08-30 16:48:36 +08:00
|
|
|
|
|
|
|
point = GEOSGeometry("SRID=4326;POINT(9.052734375 42.451171875)")
|
|
|
|
form = PointForm(data={"p": point})
|
|
|
|
self.assertIn(escape(point.json), form.as_p())
|
|
|
|
|
2013-09-02 20:15:04 +08:00
|
|
|
CustomGeometryWidget.called = 0
|
2013-08-30 16:48:36 +08:00
|
|
|
widget = form.fields["p"].widget
|
|
|
|
# Force deserialize use due to a string value
|
|
|
|
self.assertIn(escape(point.json), widget.render("p", point.json))
|
|
|
|
self.assertEqual(widget.deserialize_called, 1)
|
2013-09-03 19:51:55 +08:00
|
|
|
|
|
|
|
form = PointForm(data={"p": point.json})
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
self.assertEqual(form.cleaned_data["p"].srid, 4326)
|
2022-08-12 18:18:51 +08:00
|
|
|
|
|
|
|
def test_deprecated_width_and_height(self):
|
|
|
|
class CustomGeometryWidget(forms.BaseGeometryWidget):
|
|
|
|
map_height = 300
|
|
|
|
map_width = 550
|
|
|
|
|
|
|
|
msg = (
|
|
|
|
"The map_height and map_width widget attributes are deprecated. Please use "
|
|
|
|
"CSS to size map widgets."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
|
|
|
|
CustomGeometryWidget()
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
|
|
|
|
forms.BaseGeometryWidget({"map_width": 400})
|
|
|
|
with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
|
|
|
|
forms.BaseGeometryWidget({"map_height": 600})
|