opengis: add tile endpoint (calls GetMap) (#19603)
This commit is contained in:
parent
67a8e5ba4b
commit
937eb95926
|
@ -25,7 +25,8 @@ Depends: ${python:Depends},
|
|||
python-suds,
|
||||
python-cmislib (>= 0.5), python-cmislib (< 0.6),
|
||||
python-lxml,
|
||||
python-dateutil
|
||||
python-dateutil,
|
||||
python-pyproj
|
||||
Recommends: python-soappy, python-phpserialize
|
||||
Suggests: python-sqlalchemy, python-mako
|
||||
Description: Uniform access to multiple data sources and services (Python module)
|
||||
|
|
|
@ -14,10 +14,14 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import math
|
||||
import xml.etree.ElementTree as ET
|
||||
from django.utils.text import slugify
|
||||
|
||||
import pyproj
|
||||
|
||||
from django.db import models
|
||||
from django.http import HttpResponse
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from passerelle.base.models import BaseResource
|
||||
|
@ -70,3 +74,48 @@ class OpenGIS(BaseResource):
|
|||
response = self.requests.get(self.service_root_url, params=params)
|
||||
element = ET.fromstring(response.content)
|
||||
return {'err': 0, 'data': build_dict_from_xml(element)}
|
||||
|
||||
# https://carton.entrouvert.org/hydda-tiles/16/33650/23378.pn
|
||||
@endpoint(name='tile', pattern=r'^(?P<zoom>\d+)/(?P<tile_x>\d+)/(?P<tile_y>\d+).png',
|
||||
example_pattern='{zoom}/{tile_x}/{tile_y}.png',
|
||||
parameters={
|
||||
'zoom': {'description': _('Zoom Level'), 'example_value': '16'},
|
||||
'tile_x': {'description': _('X Coordinate'), 'example_value': '33650'},
|
||||
'tile_y': {'description': _('Y Coordinate'), 'example_value': '23378'},
|
||||
})
|
||||
def tile(self, request, zoom, tile_x, tile_y):
|
||||
zoom = int(zoom)
|
||||
|
||||
tile_x = int(tile_x)
|
||||
tile_y = int(tile_y) + 1
|
||||
|
||||
def num2deg(xtile, ytile, zoom):
|
||||
# http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Python
|
||||
n = 2.0 ** zoom
|
||||
lon_deg = xtile / n * 360.0 - 180.0
|
||||
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
|
||||
lat_deg = math.degrees(lat_rad)
|
||||
return (lon_deg, lat_deg)
|
||||
|
||||
wgs84 = pyproj.Proj(init='epsg:4326')
|
||||
epsg3857 = pyproj.Proj(init='epsg:3857')
|
||||
tl_lon, tl_lat = pyproj.transform(wgs84, epsg3857, *num2deg(tile_x, tile_y, zoom))
|
||||
br_lon, br_lat = pyproj.transform(wgs84, epsg3857, *num2deg(tile_x+1, tile_y+1, zoom))
|
||||
bbox = '%s,%s,%s,%s' % (tl_lon, tl_lat, br_lon, br_lat)
|
||||
|
||||
params = {
|
||||
'VERSION': '1.1.1',
|
||||
'SERVICE': 'WMS',
|
||||
'REQUEST': 'GetMap',
|
||||
'LAYERS': self.query_layer,
|
||||
'STYLES': '',
|
||||
'FORMAT': 'image/png',
|
||||
'TRANSPARENT': 'false',
|
||||
'HEIGHT': '256',
|
||||
'WIDTH': '256',
|
||||
'SRS': 'EPSG:3857',
|
||||
'BBOX': bbox,
|
||||
}
|
||||
return HttpResponse(
|
||||
self.requests.get(self.service_root_url, params=params).content,
|
||||
content_type='image/png')
|
||||
|
|
|
@ -6,4 +6,5 @@ requests
|
|||
-e git+http://repos.entrouvert.org/gadjo.git/#egg=gadjo
|
||||
SOAPpy
|
||||
phpserialize
|
||||
pyproj
|
||||
suds
|
||||
|
|
1
setup.py
1
setup.py
|
@ -97,6 +97,7 @@ setup(name='passerelle',
|
|||
'pyexcel-ods >= 0.4, <0.5',
|
||||
'pyexcel-xls >= 0.4, <0.5',
|
||||
'cmislib >= 0.5, <0.6',
|
||||
'pyproj',
|
||||
'feedparser',
|
||||
'lxml',
|
||||
'python-dateutil',
|
||||
|
|
|
@ -42,3 +42,11 @@ def test_feature_info(mocked_get, app, connector):
|
|||
mocked_get.return_value = utils.FakedResponse(content=FAKE_FEATURE_INFO, status_code=200)
|
||||
resp = app.get(endpoint, params={'lat': '45.796890', 'lon': '4.784140'})
|
||||
assert resp.json['data']['cad_cadastrecadparcelle_layer']['cad_cadastrecadparcelle_feature']['natureproprietaire'] == 'Particulier'
|
||||
|
||||
@mock.patch('passerelle.utils.LoggedRequest.get')
|
||||
def test_tile(mocked_get, app, connector):
|
||||
endpoint = utils.generic_endpoint_url('opengis', 'tile', slug=connector.slug)
|
||||
assert endpoint == '/opengis/test/tile'
|
||||
mocked_get.return_value = utils.FakedResponse(content='\x89PNG\r\n\x1a\n\x00\x00...', status_code=200)
|
||||
resp = app.get(endpoint + '/16/33650/23378.png')
|
||||
assert resp.content == '\x89PNG\r\n\x1a\n\x00\x00...'
|
||||
|
|
Loading…
Reference in New Issue