13 |
13 |
#
|
14 |
14 |
# You should have received a copy of the GNU Affero General Public License
|
15 |
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 |
16 |
|
17 |
17 |
import html
|
18 |
18 |
import math
|
19 |
19 |
import xml.etree.ElementTree as ET
|
20 |
20 |
|
21 |
|
import pyproj
|
22 |
21 |
import six
|
23 |
22 |
from django.contrib.postgres.fields import JSONField
|
24 |
23 |
from django.core.cache import cache
|
25 |
24 |
from django.db import models, transaction
|
26 |
25 |
from django.http import HttpResponse
|
27 |
26 |
from django.shortcuts import get_object_or_404
|
28 |
27 |
from django.template import Context, Template
|
29 |
28 |
from django.urls import reverse
|
30 |
29 |
from django.utils import timezone
|
31 |
30 |
from django.utils.text import slugify
|
32 |
31 |
from django.utils.translation import ugettext_lazy as _
|
|
32 |
from pyproj import CRS, Transformer
|
33 |
33 |
|
34 |
34 |
from passerelle.base.models import BaseQuery, BaseResource
|
35 |
35 |
from passerelle.utils.api import endpoint
|
36 |
36 |
from passerelle.utils.conversion import num2deg, simplify
|
37 |
37 |
from passerelle.utils.jsonresponse import APIError
|
38 |
38 |
|
39 |
39 |
|
40 |
40 |
def build_dict_from_xml(elem):
|
... | ... | |
245 |
245 |
return None
|
246 |
246 |
content = exception_text.text
|
247 |
247 |
content = html.unescape(content)
|
248 |
248 |
raise APIError(u'OpenGIS Error: %s' % exception_code or 'unknown code', data={'text': content})
|
249 |
249 |
|
250 |
250 |
def convert_coordinates(self, lon, lat, reverse=False):
|
251 |
251 |
lon, lat = float(lon), float(lat)
|
252 |
252 |
if self.projection != 'EPSG:4326':
|
253 |
|
wgs84 = pyproj.Proj('EPSG:4326')
|
254 |
|
target_projection = pyproj.Proj(self.projection)
|
|
253 |
wgs84 = CRS('EPSG:4326')
|
|
254 |
target_projection = CRS(self.projection)
|
255 |
255 |
if reverse:
|
256 |
|
lat, lon = pyproj.transform(target_projection, wgs84, lon, lat)
|
|
256 |
transformer = Transformer.from_crs(target_projection, wgs84)
|
|
257 |
lat, lon = transformer.transform(lon, lat)
|
257 |
258 |
else:
|
258 |
|
lon, lat = pyproj.transform(wgs84, target_projection, lat, lon)
|
|
259 |
transformer = Transformer.from_crs(wgs84, target_projection)
|
|
260 |
lon, lat = transformer.transform(lat, lon)
|
259 |
261 |
return lon, lat
|
260 |
262 |
|
261 |
263 |
def get_bbox(self, lon1, lat1, lon2, lat2):
|
262 |
264 |
if self.projection == 'EPSG:4326':
|
263 |
265 |
# send as is but invert coordinates
|
264 |
266 |
return '%.6f,%.6f,%.6f,%.6f' % (lat1, lon1, lat2, lon2)
|
265 |
|
wgs84 = pyproj.Proj('EPSG:4326')
|
266 |
|
target_projection = pyproj.Proj(self.projection)
|
267 |
|
x1, y1 = pyproj.transform(wgs84, target_projection, lat1, lon1)
|
268 |
|
x2, y2 = pyproj.transform(wgs84, target_projection, lat2, lon2)
|
|
267 |
wgs84 = CRS('EPSG:4326')
|
|
268 |
target_projection = CRS(self.projection)
|
|
269 |
transformer = Transformer.from_crs(wgs84, target_projection)
|
|
270 |
x1, y1 = transformer.transform(lat1, lon1)
|
|
271 |
x2, y2 = transformer.transform(lat2, lon2)
|
269 |
272 |
return '%.6f,%.6f,%.6f,%.6f' % (x1, y1, x2, y2)
|
270 |
273 |
|
271 |
274 |
@endpoint(
|
272 |
275 |
perm='can_access',
|
273 |
276 |
description=_('Get feature info'),
|
274 |
277 |
parameters={
|
275 |
278 |
'lat': {'description': _('Latitude'), 'example_value': '45.79689'},
|
276 |
279 |
'lon': {'description': _('Longitude'), 'example_value': '4.78414'},
|
277 |
|
-
|