[英]How to create a HTML world map with GeoDjango?
The GeoDjango tutorial explains how to insert world borders into a spatial database. GeoDjango教程介绍了如何将世界边界插入空间数据库。
I would like to create a world Map in HTML with these data, with both map
and area
tags. 我想使用HTML和这些数据以及map
和area
标签创建一个世界地图。 Something like that . 这样的东西 。
I just don't know how to retrieve the coordinates for each country (required for the area
's coords
attribute). 我只是不知道如何检索每个国家/ area
的coords
(该area
的coords
属性需要此coords
)。
from world.models import WorldBorders
for country in WorldBorders.objects.all():
print u'<area shape="poly" title="%s" alt="%s" coords="%s" />' % (v.name, v.name, "???")
Thanks ! 谢谢 !
In the worldborders example, the attribute mpoly
is where the geographic polygon is actually stored. 在worldborders示例中,属性mpoly
是实际存储地理多边形的位置。
In your example, you're going to want to access v.mpoly
在您的示例中,您将要访问v.mpoly
You're not going to be able to use it directly however because mpoly
is itself a MultiPolygon
field . 但是,您将无法直接使用它,因为mpoly
本身是一个MultiPolygon
字段 。 Consider a country like Canada that has a bunch of islands, each island and the main landmass is a polygon. 考虑一个像加拿大这样的国家,它有一堆岛屿,每个岛屿和主要陆地都是一个多边形。 So to arrive at your points and a complete description of Canada's borders you need to: 因此,要获得您的观点以及对加拿大边界的完整描述,您需要:
To use lat/lon in an SVG, you need to project them into pixel (x/y) space. 要在SVG中使用纬度/经度,您需要将其投影到像素(x / y)空间中。 A simple transformation might look like this: 一个简单的转换可能看起来像这样:
>>> x = (lon + 180) / 360 * image_width
>>> y = (90 - lat) / 180 * image_height
For an image where image_width == 2 * image_height
, this will give you something like the map at the link posted (which looks like an equirectangular projection ). 对于image_width == 2 * image_height
的图像,这将为您提供类似于发布的链接处的地图(看起来像是等角投影 )。
To use a different projection (eg Mercator ), use the GEOSGeometry.transform method in GeoDjango before applying the transform. 要使用其他投影(例如Mercator ),请在应用转换之前使用GeoDjango中的GEOSGeometry.transform方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.