PostGis Polygon使用django进入传单地图

问题描述 投票:0回答:1

我的问题是我不知道如何使用django将PostGIS中存储的多边形可视化为传单地图。

在模板中我用它来绘制点:

  <script type="text/javascript">

      function map_init(map, options) {
        {% for each_model in loca %}

          var lon =   {{each_model.location.x}}
          var lat =   {{each_model.location.y}}


          map.setView([lat, lon], 2);

          L.marker([lat, lon]).bindPopup("{{each_model.name}}").addTo(map)

          {% endfor %}
          // get point lat and lon

          // zoom to point & add it to map
        ;
      }

  </script>

models.朋友

from __future__ import unicode_literals

from django.contrib.gis.db import models
from django.contrib.gis.geos import Point


class Shop(models.Model):
    name = models.CharField(max_length=100)
    location = models.PointField()
    address = models.CharField(max_length=100)
    city = models.PolygonField()

如果我打印{{loca.city}},它将打印出GEOS对象,如下所示:

SRID=4326;POLYGON ((2.988282590688516 6............))

任何想法我如何绘制多边形,因为我正在绘制点?

更新II:

使用Shop.object.all()打印出模型生成的内容

<QuerySet [<Shop: Shop object (1)>, <Shop: Shop object (2)>, <Shop: Shop object (3)>, <Shop: Shop object (4)>]>

数据库表看起来像存储多边形和点(x,y)enter image description here

我考虑过手动使用查询(在视图中),但它不起作用(cursor.execute(“SELECT ST_AsText(city)FROM trial2_shop;)

使用此代码:

{% for each_model in loca %}

{{each_model.name}} <br>

{% endfor %}

输出是这样的:

HELLO 
PARIS 
France 
abc 

然后使用这个:

{% for each_model in loca %}

{{each_model.city}} <br>

{% endfor %}

输出是这样的:

SRID=4326;POLYGON ((2.988282590688516 6.287501448673132, -2.724608033516236 4.362344411133384, -5.624998658112493 4.800392361058798, -9.316404907598319 4.62520548641869, -7.29492053288002 -2.927082610924476, 8.701173214893267 -0.8189537900473602, 9.667970089758358 3.573231060257538, 5.537110715333732 4.362344411133384, 2.988282590688516 6.287501448673132)) 
SRID=4326;POLYGON ((2.379232127237539 48.99532183070828, 1.835408885125764 48.778601581576, 2.489095408472194 48.61544474629894, 2.780233103744227 48.87985458449839, 2.379232127237539 48.99532183070828)) 
SRID=4326;POLYGON ((4.921880363732911 48.86996904931716, 13.00781786260736 44.28238994198036, -8.08593213445636 32.47943349940858, -25.31249463205829 49.55890707900826, 5.364418038408538e-06 58.4058966101036, -8.789057134358529 49.10068218084409, 4.921880363732911 48.86996904931716)) 
SRID=4326;POLYGON ((10.10742321469805 59.8969527006269, 16.34765758882955 60.31310053764656, 15.02929821401315 57.87662999493084, 12.39257946438035 57.92333496310473, 10.10742321469805 59.8969527006269))

所以我确定存储的是多边形,但在检索时将其绘制在地图模板中是个问题...

python leaflet postgis geodjango geos
1个回答
0
投票

您基本上需要在模板中使用leaflet polygon documentation。 你需要2件事来做这件事:

  1. Polygon's coordinates列表: lst_coords = [[point.x, point.y] for point in shop_object.city] 这将产生列表/坐标对的列表。
  2. 将坐标列表插入到传单多边形中: L.polygon(lst_coords, {color: 'red'}).addTo(map);

现在让我们将上面的内容应用到您的示例中:

  1. property添加到模型中以返回坐标列表(仅为方便起见): class Shop(models.Model): name = models.CharField(max_length=100) location = models.PointField() address = models.CharField(max_length=100) city = models.PolygonField() @property def city_coords_list(self): return [[point.x, point.y] for point in self.city]
  2. 将以上内容集成到模板中: <script type="text/javascript"> function map_init(map, options) { {% for each_model in loca %} L.polygon( {{ each_model.city_coords_list }}, {color: 'red'} ).bindPopup({{ each_model.name }}).addTo(map) {% endfor %} // Find a way to fit the map accordignly (search the docs) ; }
© www.soinside.com 2019 - 2024. All rights reserved.