Python GeoJSON数据不';不包含有意义的数据
我正在使用Python GeoJSON数据不';不包含有意义的数据,python,django,python-3.x,geojson,geodjango,Python,Django,Python 3.x,Geojson,Geodjango,我正在使用vectorformats在我的地图上显示GeoDjango数据,如下所示。我的views.py文件中有这个: def geojsonFeed(request): querySet = WorldBorder.objects.filter() djf = Django.Django(geodjango="mpoly", properties=['name', 'iso3']) geoj = GeoJSON.GeoJSON() s = geoj.encod
vectorformats
在我的地图上显示GeoDjango数据,如下所示。我的views.py文件中有这个:
def geojsonFeed(request):
querySet = WorldBorder.objects.filter()
djf = Django.Django(geodjango="mpoly", properties=['name', 'iso3'])
geoj = GeoJSON.GeoJSON()
s = geoj.encode(djf.decode(querySet))
return HttpResponse(s)
但反应是这样的
["type", "features", "crs"]
有人能帮我找出我的代码出了什么问题吗
更新:添加了WorldBorder模型
class WorldBorder(models.Model):
# Regular Django fields corresponding to the attributes in the
# world borders shapefile.
name = models.CharField(max_length=50)
area = models.IntegerField()
pop2005 = models.IntegerField('Population 2005')
fips = models.CharField('FIPS Code', max_length=2)
iso2 = models.CharField('2 Digit ISO', max_length=2)
iso3 = models.CharField('3 Digit ISO', max_length=3)
un = models.IntegerField('United Nations Code')
region = models.IntegerField('Region Code')
subregion = models.IntegerField('Sub-Region Code')
lon = models.FloatField()
lat = models.FloatField()
# GeoDjango-specific: a geometry field (MultiPolygonField)
mpoly = models.MultiPolygonField()
# Returns the string representation of the model.
def __str__(self):
return self.name
我正在使用Django 2.1.7
更新2:
>>> print(querySet)
<QuerySet [<WorldBorder: Antigua and Barbuda>, <WorldBorder: Algeria>, <WorldBorder: Azerbaijan>, <WorldBorder: Albania>, <WorldBorder: Anguilla>, <WorldBorder: Armenia>, <WorldBorder: Angola>, <WorldBorder: American Samoa>, <WorldBorder: Argentina>, <WorldBorder: Australia>, <WorldBorder: Andorra>, <WorldBorder: Gibraltar>, <WorldBorder: Bahrain>, <WorldBorder: Barbados>, <WorldBorder: Bermuda>, <WorldBorder: Bahamas>, <WorldBorder: Bangladesh>, <WorldBorder: Brunei Darussalam>, <WorldBorder: Canada>, <WorldBorder: Cambodia>, '...(remaining elements truncated)...']>
打印(查询集)
确认非空查询集后编辑:
我发现了这个问题,它与vectorformats
模块的核心代码有关
具体来说,在GeoJSON.encode
中:
列表()
导致问题
让我们用一个简单的例子来重现这个问题:
>>> import json
>>> test = {'a': 5, 'b': [1, 2, 3], 'c': {'e': 2, 'f': 5}}
>>> list(test)
['a', 'b', 'c']
这里我们看到的行为与问题中的行为完全相同。让我们更进一步:
>>> json.dumps(list(test))
'["a", "b", "c"]'
但是没有列表()
:
因此,围绕此问题有两种可能的解决方案:
vectorformat
代码删除list()
调用to_string=False
调用encode
方法,并按如下方式“jsonify”生成的字典:
import json
def geojsonFeed(request):
queryset = WorldBorder.objects.all()
djf = Django.Django(geodjango="mpoly", properties=['name', 'iso3'])
geoj = GeoJSON.GeoJSON()
s = geoj.encode(djf.decode(queryset), to_string=False)
return HttpResponse(json.dumps(s))
通过快速学习您的模块,它似乎按照预期工作,因此这不是原因。
看看这个方法:
result\u data
具有结构[“type”、“features”、“crs”]
,它被转换为json列表,因为默认情况下,您有to\u string
参数True
我能想象出出现问题的唯一原因是querySet=WorldBorder.objects.filter()
query是空的
顺便问一下,通过使用不带参数的
filter()
,您可以得到与all()
查询类似的结果。是否有理由使用一个似乎已被放弃且非常陈旧的模块(vectorformats
)(PyPi页面:告诉我们该软件包来自2009年,并且在版本0.1中卡住了)?您在项目中使用的Django版本是什么?另外,你能分享你问题中的WorldBorder
模型吗?谢谢@JohnMoutafis。虽然vectorformats被放弃了,但我已经在我的项目中作为Git子模块重用和使用了它。请看问题的最新版本。谢谢你,@John,谢谢你的帮助print(querySet)
返回以下内容:print(querySet)
。我更新了问题。@sk001我找到了它:D。我在我的答案中添加了一个解释和修复问题的编辑!谢谢你,约翰。现在没事了。非常感谢。
>>> json.dumps(test)
'{"a": 5, "b": [1, 2, 3], "c": {"e": 2, "f": 5}}'
import json
def geojsonFeed(request):
queryset = WorldBorder.objects.all()
djf = Django.Django(geodjango="mpoly", properties=['name', 'iso3'])
geoj = GeoJSON.GeoJSON()
s = geoj.encode(djf.decode(queryset), to_string=False)
return HttpResponse(json.dumps(s))
def encode(self, features, to_string=True, **kwargs):
"""
Encode a list of features to a JSON object or string.
to_string determines whethr it should convert the result to
a string or leave it as an object to be encoded later
"""
results = []
result_data = None
for feature in features:
data = self.encode_feature(feature)
for key,value in data['properties'].items():
if value and isinstance(value, str):
data['properties'][key] = str(value)
results.append(data)
result_data = {
'type':'FeatureCollection',
'features': results,
'crs': self.crs
}
if to_string:
result = json_dumps(list(result_data))
else:
result = result_data
return result