Commit 2b379ac4 authored by Serge S. Koval's avatar Serge S. Koval Committed by GitHub

Merge pull request #1362 from jmagnusson/revert-mapbox-flip-coordinates

Revert PR #1231
parents f9840980 9679330c
import warnings
import geoalchemy2
from flask import current_app
from shapely.geometry import shape
from sqlalchemy import func
......@@ -25,37 +22,16 @@ class GeoJSONField(JSONField):
self.geometry_type = geometry_type.upper()
self.session = session
def _flip_coordinates(self, other_func):
if current_app.config.get('MAPBOX_FIX_COORDINATES_ORDER'):
return func.ST_FlipCoordinates(other_func)
else:
warnings.warn(
'Consider setting the Flask config option '
'MAPBOX_FIX_COORDINATES_ORDER as the current implementation '
'passes lng/lat coordinates in the wrong order to '
'Leaflet. Without this setting any coordinates saved will '
'have flipped coordinates in your database. '
'Please note that this will become the standard behavior in '
'the next major version of Flask-Admin.'
)
return other_func
def _value(self):
if self.raw_data:
return self.raw_data[0]
if type(self.data) is geoalchemy2.elements.WKBElement:
if self.srid is -1:
return self.session.scalar(
func.ST_AsGeoJson(
self._flip_coordinates(self.data)
)
)
return self.session.scalar(func.ST_AsGeoJson(self.data))
else:
return self.session.scalar(
func.ST_AsGeoJson(
self._flip_coordinates(
func.ST_Transform(self.data, self.web_srid)
)
func.ST_Transform(self.data, self.web_srid)
)
)
else:
......@@ -68,14 +44,12 @@ class GeoJSONField(JSONField):
if self.data is not None:
web_shape = self.session.scalar(
func.ST_AsText(
self._flip_coordinates(
func.ST_Transform(
func.ST_GeomFromText(
shape(self.data).wkt,
self.web_srid
),
self.transform_srid
)
func.ST_Transform(
func.ST_GeomFromText(
shape(self.data).wkt,
self.web_srid
),
self.transform_srid
)
)
)
......
......@@ -125,51 +125,6 @@ def test_model():
eq_(db.session.query(GeoModel).count(), 0)
def test_mapbox_fix_point_coordinates():
app, db, admin = setup()
app.config['MAPBOX_FIX_COORDINATES_ORDER'] = True
GeoModel = create_models(db)
db.create_all()
GeoModel.query.delete()
db.session.commit()
view = ModelView(GeoModel, db.session)
admin.add_view(view)
# Make some test clients
client = app.test_client()
rv = client.post('/admin/geomodel/new/', data={
"name": "test1",
"point": '{"type": "Point", "coordinates": [125.8, 10.0]}',
"line": '{"type": "LineString", "coordinates": [[50.2345, 94.2], [50.21, 94.87]]}',
"polygon": '{"type": "Polygon", "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]}',
"multi": '{"type": "MultiPoint", "coordinates": [[100.0, 0.0], [101.0, 1.0]]}',
})
model = db.session.query(GeoModel).first()
# Notice how the coordinates are reversed here, i.e. longitude first which
# is the way it's stored in PostGIS columns.
eq_(list(to_shape(model.point).coords), [(10.0, 125.8)])
eq_(list(to_shape(model.line).coords), [(94.2, 50.2345), (94.87, 50.21)])
eq_(list(to_shape(model.polygon).exterior.coords),
[(0.0, 100.0), (0.0, 101.0), (1.0, 101.0), (1.0, 100.0), (0.0, 100.0)])
eq_(list(to_shape(model.multi).geoms[0].coords), [(0.0, 100.0)])
eq_(list(to_shape(model.multi).geoms[1].coords), [(1.0, 101.0)])
rv = client.get('/admin/geomodel/')
eq_(rv.status_code, 200)
html = rv.data.decode('utf-8')
pattern = r'(.|\n)+({.*"type": ?"Point".*})</textarea>(.|\n)+'
group = re.match(pattern, html).group(2)
p = json.loads(group)
# Reversed order again, so that it's parsed correctly by leaflet
eq_(p['coordinates'][0], 10.0)
eq_(p['coordinates'][1], 125.8)
def test_none():
app, db, admin = setup()
GeoModel = create_models(db)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment