TransWikia.com

Loading multiple shapefiles in GeoDjango?

Geographic Information Systems Asked by neferpitou on August 29, 2021

I have 3 three shapefiles and would like to upload all of them and project the result in GeoDjango admin.

a) state.shp (for the shape of a state)
b) boundary.shp (to identify a specific portion in the state.shp)
c) beat.shp (to further sub divide the region obtained from boundary.shp file)

What I want to do is the following:

Uploading state.shp gives the following result.

enter image description here

Uploading boundary.shp gives the following result:

enter image description here
Please note new operations are performed on results obtained by previous operations. shapefile can be of different types (i.e poly, lines or points).

And similarly any other shapefile file which is uploaded should be projected. After following the official documentation I can project single shapefile in GeoDjango admin.

If I create separate models for each shapefile I will not get combined result.

How can I display result of multiple shapefile in GeoDjango?

EDIT: I want my end result to look something like this (load all shapefiles on openstreetmap in GeoDjango)

enter image description here

But however I can load single shapefile only (one shapefile at a time). Something like this

state.shp
enter image description here
boundary.shp
enter image description here

I would like to combine both of my results in a single result as shown in QGIS output.

One Answer

In my case I created a template where I load both data in the form of geojson using url created in url.py

#views.py
from django.shortcuts import render
from django.views.generic import TemplateView
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import County

# Create your views here.

class HomePageView(TemplateView):
    template_name='index.html'  #the template name in template folder


#state dataset

def state_datasets(request):
    state=serialize('geojson',State.objects.all()) #converts the shapefile loaded to geojson
    return HttpResponse(state,content_type='application/json')

#beat dataset


def beat_datasets(request):
    beat=serialize('geojson',Beat.objects.all()) #converts the shapefile loaded to geojson
    return HttpResponse(beat,content_type='application/json')


#urls.py

from django.conf.urls import url
from djgeojson.views import GeoJSONLayerView
from .views import HomePageView,beat_datasets,state_dataset




urlpatterns=[
    url(r'^$',HomePageView.as_view(),name='home'),
    url(r'^state_data/$',state_datasets,name='state') #state url
url(r'^beat_data/$',beat_datasets,name='beat') #the url beat
]

Make sure you add this line below in your setting at the template:

 'DIRS': [os.path.join(BASE_DIR,'templates')],

Then create a templates folder with index.html file:

#index.html

<!DOCTYPE html>
<html lang="en">

{% load static %}
{% load leaflet_tags %}

<head>

    {% leaflet_js %}
    {% leaflet_css %}
   
    <title>map</title>
  
<style>
    #gis{
        width:76%;
        height:550px;
    }
</style>

   <script src="{% static 'ajax/leaflet.ajax.js' %}"></script>
   

</head>

<body>
          <script>
                function our_layers(map,options){

                    // adding open street map

                    var osm = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}{y}{x}.png');
                    // loading state geojson

                    var state = new L.GeoJSON.AJAX("{% url 'state' %}",{

                    });
                    state.addTo(map);

                     var beat = new L.GeoJSON.AJAX("{% url 'beat' %}",{

                    });
                    beat.addTo(map);


                }
        </script>

   {% leaflet_map "gis" callback="window.our_layers" %}

</body>
</html>


#make sure you have leaflet ajax plugin ,django-leaflet (pip install django-leaflet)

Correct answer by Francis Odero on August 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP