gpt4 book ai didi

django - 如何使用 View 从 PostGIS 中提取几何图形,然后使用 Django 将其添加到模板中的传单 map

转载 作者:行者123 更新时间:2023-12-02 16:48:20 26 4
gpt4 key购买 nike

我想在 View 中使用 Python 从 PostGIS 数据库中提取多边形几何数据,并将其添加到模板中的传单 map 中。最简单的方法似乎是在我的 Django View 中使用 postgis 函数 ST_AsGeoJSON 提取数据并将其转换为 GeoJSON,然后将其作为 L.geoJSON(GEOJSON).addTo(map) 函数中的上下文呈现给模板。

这是行不通的。在请求 map 页面时, map 现在是空白的,似乎无法识别 GeoJSON。我已经能够从 View 中传递硬编码的多边形并将其添加到 map 中,但我的 postgis 数据库中的几何数据根本无效。

这是一个带有成功打印在 map 上的硬编码多边形的 View :

from django.shortcuts import render

def map_view(request, *args, **kwargs):
geo_json={
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-0.10746002197265625,
51.505537109466715
],
[
-0.11466979980468751,
51.498377681772325
],
[
-0.0968170166015625,
51.493568479510415
],
[
-0.09080886840820312,
51.502438390761164
],
[
-0.10746002197265625,
51.505537109466715
]
]
]
}
}
]
}

return render(request ,'map.html', {'geo_json': geo_json})

map 模板如下所示:

<!DOCTYPE html>
<html>
<head>
<title>Map Page</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<style>
#map { position: relative;
width: 600px;
height: 775px;
border: 3px solid #000000;}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map').setView([54.8,-4.45],6);

L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=9GKOA9jJ3jCIWFUd8k00', {attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>',}).addTo(map);

L.geoJSON({{ geo_json | safe }}).addTo(map);

</script>

</body>
</html>

Here is the leaflet map with the polygon added

现在,当我尝试使用我的新 View 从我的 postgis 数据库中获取 GeoJSON 时,它不起作用:

import psycopg2
from django.shortcuts import render

def map_view(request, *args, **kwargs):

connection = psycopg2.connect(database="electio5_geekdata",user="electio5_blake", password="dummypassword", host='localhost')
cursor = connection.cursor()
cursor.execute("select st_AsGeoJSON(shape) from boris_constituency limit 1")
varpoly=cursor.fetchall()

geo_json={
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": varpoly
}
]
}

return render(request ,'map.html', {'geo_json': geo_json})

我注意到 GeoJSON 的格式在我输出时略有不同:-

很好

{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {}, 'geometry': {'type': 'Polygon', 'coordinates' and等等等等

是个问题

{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {}, 'geometry': [('{"type":"MultiPolygon","坐标”等等等等

有问题的 GeoJSON 在第二个“类型”键之前有额外的括号和引号

所以我的问题是:-

1/是否可以重新格式化有问题的 GeoJSON?我很难去掉包裹列表的不需要的字符

2/或者我可以只提取坐标并将其传递给 geo_json 的相关部分吗?

3/或者我可以从 postgis 中提取多边形数据并将其添加到传单 map 中的任何方式

顺便说一句,您可能想知道为什么我使用游标而不是使用具有 GeoJSON 方法的 Django 模型对象。由于库未正确配置,这种方法给了我一个 GDAL 错误,这是另一天的问题!

非常感谢您对此的关注。

菲尔#anoobintrouble

最佳答案

正如您将在上面的评论中看到的那样,“cabesuon”给了我答案。但我会整合一切,希望能帮助 future 的用户。您可以通过以下方法从 PostGIS 数据库中提取几何(多边形)数据,然后将其呈现为模板并使用 Django 网络框架将其添加到传单 map 。

下面的 View 提取数据,将其转换为 GeoJSON,然后将其返回给 map.html 模板:-

from django.shortcuts import render
import json
import psycopg2

def map_view(request, *args, **kwargs):

connection = psycopg2.connect(database="electio5_geekdata",user="electio5_blake", password="adummypassword", host='localhost')
cursor = connection.cursor()
cursor.execute("select name, st_AsGeoJSON(shape) from boris_constituency limit 1")

varcons=cursor.fetchone()

geo_json={
"type": "Feature",
"name": varcons[0],
"properties": {},
"geometry": json.loads(varcons[1])
}

return render(request ,'map.html', {'geo_json': geo_json})

下面的 map 模板获取 geo_json 上下文并将其添加到传单 map ,绘制多边形几何图形(在本例中是英国议会选区“Aldershot”):-

<!DOCTYPE html>
<html>
<head>
<title>Map Page</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<style>
#map { position: relative;
width: 600px;
height: 775px;
border: 3px solid #000000;}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = L.map('map').setView([54.8,-4.45],6);

L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=9GKOA9jJ3jCIWFUd8k00', {attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>',}).addTo(map);

L.geoJSON({{ geo_json | safe }}).addTo(map);

</script>

{{ geo_json }}

</body>
</html>

here is the geometry plotted on the map

一些注意事项:-

1/我使用纯 python 访问数据库。我真的应该使用 GeoDjango ,像这样调用几何:-

from boris.models import constituency
obj=constituency.objects.get(id=1)
geo_json=obj.shape.geojson

但是得到了一个 GDAL_ERROR 1: b'PROJ: proj_create_from_database: Cannot find proj.db'

我认为 GDAL 库配置不正确 - 我可能会尝试修复此问题

2/我现在打算让这个应用程序具有交互性,让用户选择选区、地区或根据他们的位置找到他们的选区,并尽可能多地返回有趣的信息。

请随意发表评论、更正并添加任何有用的内容。

菲尔#anoobinneed

关于django - 如何使用 View 从 PostGIS 中提取几何图形,然后使用 Django 将其添加到模板中的传单 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59651110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com