使用django将PostGis多边形转换成传单地图

时间:2019-04-14 19:19:23

标签: python leaflet postgis geodjango geos

我的问题是我不知道如何使用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.py

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))

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

1 个答案:

答案 0 :(得分:0)

您基本上需要在模板中使用leaflet polygon documentation
您需要完成以下两项工作:

  1. Polygon's coordinates的列表:

    DefaultError

    这将产生一个列表列表/坐标对。

  2. 将坐标列表插入到传单多边形中:

    // Before
    if(avformat_open_input(&pFormatCtx, movie_file, NULL, NULL)!=0)
        return -1; // Couldn't open file
    
    // After
    AVInputFormat *pAVInputFormat = NULL;
    pAVInputFormat = av_find_input_format("x11grab");
    if(avformat_open_input(&pFormatCtx,, ":0.0+10,250", pAVInputFormat, NULL))
        return -1; // Couldn't open file
    

现在让我们将以上内容应用于您的示例:

  1. 在模型中添加lst_coords = [[point.x, point.y] for point in shop_object.city] 以返回坐标列表(仅出于方便起见):

    L.polygon(lst_coords, {color: 'red'}).addTo(map);
    
  2. 将以上内容集成到模板中:

    property