map tile rendering base
This commit is contained in:
parent
99f9aac0c3
commit
5cad398951
5 changed files with 59 additions and 0 deletions
1
src/c3nav/mapdata/render/__init__.py
Normal file
1
src/c3nav/mapdata/render/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from c3nav.mapdata.render.svg import render_svg # noqa
|
9
src/c3nav/mapdata/render/svg.py
Normal file
9
src/c3nav/mapdata/render/svg.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
from c3nav.mapdata.utils.svg import SVGImage
|
||||||
|
|
||||||
|
|
||||||
|
def render_svg(level, miny, minx, maxy, maxx, scale=1):
|
||||||
|
svg = SVGImage(bounds=((miny, minx), (maxy, maxx)), scale=scale)
|
||||||
|
|
||||||
|
# todo: render
|
||||||
|
|
||||||
|
return svg.get_xml()
|
7
src/c3nav/mapdata/urls.py
Normal file
7
src/c3nav/mapdata/urls.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
from django.conf.urls import url
|
||||||
|
|
||||||
|
from c3nav.mapdata.views import tile
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
url(r'^(?P<level>\d+)/(?P<zoom>\d+)/(?P<x>-?\d+)/(?P<y>-?\d+).(?P<format>png|svg)$', tile, name='mapdata.tile'),
|
||||||
|
]
|
40
src/c3nav/mapdata/views.py
Normal file
40
src/c3nav/mapdata/views.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from django.http import Http404, HttpResponse
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
from shapely.geometry import box
|
||||||
|
|
||||||
|
from c3nav.mapdata.models import Level, Source
|
||||||
|
from c3nav.mapdata.render import render_svg
|
||||||
|
|
||||||
|
|
||||||
|
def tile(request, level, zoom, x, y, format):
|
||||||
|
zoom = int(zoom)
|
||||||
|
if not (0 <= zoom <= 10):
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
bounds = Source.max_bounds()
|
||||||
|
|
||||||
|
x, y = int(x), int(y)
|
||||||
|
size = 256/2**zoom
|
||||||
|
minx = size * x
|
||||||
|
miny = size * y
|
||||||
|
maxx = minx + size
|
||||||
|
maxy = miny + size
|
||||||
|
|
||||||
|
if not box(bounds[0][1], bounds[0][0], bounds[1][1], bounds[1][0]).intersects(box(minx, miny, maxx, maxy)):
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
level = get_object_or_404(Level, pk=level)
|
||||||
|
|
||||||
|
svg = render_svg(level, miny, minx, maxy, maxx, scale=2**zoom)
|
||||||
|
|
||||||
|
if format == 'svg':
|
||||||
|
response = HttpResponse(svg, 'image/svg+xml')
|
||||||
|
elif format == 'png':
|
||||||
|
p = subprocess.run(('rsvg-convert', '--format', 'png'), input=svg.encode(), stdout=subprocess.PIPE, check=True)
|
||||||
|
response = HttpResponse(p.stdout, content_type="image/png")
|
||||||
|
else:
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
|
return response
|
|
@ -6,11 +6,13 @@ from django.contrib import admin
|
||||||
|
|
||||||
import c3nav.api.urls
|
import c3nav.api.urls
|
||||||
import c3nav.editor.urls
|
import c3nav.editor.urls
|
||||||
|
import c3nav.mapdata.urls
|
||||||
import c3nav.site.urls
|
import c3nav.site.urls
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^editor/', include(c3nav.editor.urls)),
|
url(r'^editor/', include(c3nav.editor.urls)),
|
||||||
url(r'^api/', include(c3nav.api.urls, namespace='api')),
|
url(r'^api/', include(c3nav.api.urls, namespace='api')),
|
||||||
|
url(r'^map/', include(c3nav.mapdata.urls)),
|
||||||
url(r'^admin/', admin.site.urls),
|
url(r'^admin/', admin.site.urls),
|
||||||
url(r'^locales/', include('django.conf.urls.i18n')),
|
url(r'^locales/', include('django.conf.urls.i18n')),
|
||||||
url(r'^', include(c3nav.site.urls)),
|
url(r'^', include(c3nav.site.urls)),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue