首页 > 解决方案 > 将geojson文件加载到django数据库中

问题描述

有人能帮助我吗?

我正在尝试将两个 geojson 文件放入我的数据库(django.contrib.gis.db.backends.postgis)但没有成功。

楷模

class Node(models.Model):
    network = models.ForeignKey(RoadNetwork, on_delete=models.CASCADE)
    node_id = models.IntegerField() 
    name = models.CharField('Node Name', max_length=200)
    location = models.PointField()

形式

class NodeForm(forms.ModelForm):
    my_file = forms.FileField()
    class Meta:
        model=Node
          fields = [
          'network',
          'my_file',
        ]

意见(这是我的看法,我认为问题出在外键上,但没有。上传文件时没有任何反应。我看不出我在哪里错了。

def upload_node(request):
    template = "upload.html"
    form = NodeForm()
    if request.method == 'POST':
        form = NodeForm(request.POST)
        if form.is_valid():
            network_fk = form.cleaned_data['nework']

            datafile = request.FILES['my_file']
            objects = json.load(datafile)
            for object in objects['features']:
                properties = object['properties']
                geometry = object['geometry']
                node_id = properties.get('id')
                name = properties.get('name', 'no-name')
                location = fromstr(geometry.get('coordinates'))
                Node(
                    node_id = node_id,
                    name = name,
                    location = location,
                    network = network_fk,
                    ).save(commit=True)

    return render(request, template, {'form':form})

geojson 文件

{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "id": 1, "name": "CBD" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 2, "name": "E - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (4.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 3, "name": "NE - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (2.8284 2.8284)" } },
{ "type": "Feature", "properties": { "id": 4, "name": "N - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 4.0)" } },
{ "type": "Feature", "properties": { "id": 5, "name": "NW - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (-2.8284 2.8284)" } },
{ "type": "Feature", "properties": { "id": 6, "name": "W - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (-4.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 7, "name": "SW - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (-2.8284 -2.8284)" } },
{ "type": "Feature", "properties": { "id": 8, "name": "S - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 -4.0)" } },
{ "type": "Feature", "properties": { "id": 9, "name": "SE - 1" }, "geometry": { "type": "Point", "coordinates": "POINT (2.8284 -2.8284)" } },
{ "type": "Feature", "properties": { "id": 10, "name": "E - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (8.0, 0.0)" } },
{ "type": "Feature", "properties": { "id": 11, "name": "NE - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (5.6569 5.6569)" } },
{ "type": "Feature", "properties": { "id": 12, "name": "N - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 8.0)" } },
{ "type": "Feature", "properties": { "id": 13, "name": "NW - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (-5.6569 5.6569)" } },
{ "type": "Feature", "properties": { "id": 14, "name": "W - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (-8.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 15, "name": "SW - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (-5.6569 -5.6569)" } },
{ "type": "Feature", "properties": { "id": 16, "name": "S - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 -8.0)" } },
{ "type": "Feature", "properties": { "id": 17, "name": "SE - 2" }, "geometry": { "type": "Point", "coordinates": "POINT (5.6569 -5.6569)" } },
{ "type": "Feature", "properties": { "id": 18, "name": "E - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (12.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 19, "name": "NE - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (8.4853 8.4853)" } },
{ "type": "Feature", "properties": { "id": 20, "name": "N - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 12.0)" } },
{ "type": "Feature", "properties": { "id": 21, "name": "NW - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (-8.4853 8.4853)" } },
{ "type": "Feature", "properties": { "id": 22, "name": "W - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (-12.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 23, "name": "SW - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (-8.4853 -8.4853)" } },
{ "type": "Feature", "properties": { "id": 24, "name": "S - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 -12.0)" } },
{ "type": "Feature", "properties": { "id": 25, "name": "SE - 3" }, "geometry": { "type": "Point", "coordinates": "POINT (8.4853 -8.4853)" } },
{ "type": "Feature", "properties": { "id": 26, "name": "E - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (16.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 27, "name": "NE - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (11.3137 11.3137)" } },
{ "type": "Feature", "properties": { "id": 28, "name": "N - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 16.0)" } },
{ "type": "Feature", "properties": { "id": 29, "name": "NW - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (-11.3137 11.3137)" } },
{ "type": "Feature", "properties": { "id": 30, "name": "W - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (-16.0 0.0)" } },
{ "type": "Feature", "properties": { "id": 31, "name": "SW - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (-11.3137 -11.3137)" } },
{ "type": "Feature", "properties": { "id": 32, "name": "S - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (0.0 -16.0)" } },
{ "type": "Feature", "properties": { "id": 33, "name": "SE - 4" }, "geometry": { "type": "Point", "coordinates": "POINT (11.3137 -11.3137)" } }
]
}

标签: pythondjangoformsgeodjango

解决方案


推荐阅读