Build Web GIS applications based on Django

PHPz
Release: 2023-06-17 13:12:03
Original
1763 people have browsed it

With the rapid development of Global Positioning System (GPS) and satellite imaging technology, Geographic Information System (GIS) has become an important application field. GIS is not limited to map production and analysis, but is also widely used in environmental management, land management, urban planning and other fields. The development of Web GIS applications allows users to query, analyze and manage GIS data at any place, at any time and through any device, which has great application prospects.

Django is a web development framework based on the Python language. It provides a series of development tools and technologies that can help us quickly build efficient web applications. This article will introduce how to use Django to build a simple Web GIS application.

1. Environment preparation

Before starting, we need to ensure that the following necessary environments have been installed:

  1. Python 3.x
  2. Django
  3. GDAL

Among them, GDAL is a commonly used geographic data processing library, we will use it to process GIS data.

2. Create a new Django project

You can create a new Django project through the following command:

django-admin startproject webgis
Copy after login

This command creates a Django project named webgis. We can enter the root directory of the project through the following command:

cd webgis
Copy after login

Next, we can create an application named gisapp through the following command:

python manage.py startapp gisapp
Copy after login

This command creates an application called gisapp Django application and create a subdirectory with the same name in the project directory.

3. Configure the Django project

We need to configure GDAL and the application in the settings.py file of the project:

# settings.py # 导入GDAL库 from django.contrib.gis import gdal # 数据库设置 DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', # 使用PostGIS数据库 'NAME': 'webgis', # 数据库名称 'USER': 'postgres', # 数据库用户名 'PASSWORD': '****', # 数据库密码 'HOST': '127.0.0.1', # 数据库地址 'PORT': '5432', # 数据库端口 } } # 应用设置 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.gis', 'gisapp', # 加入我们的应用程序 ] # 时间区域设置 TIME_ZONE = 'Asia/Shanghai' # GDAL设置 gdal.HAS_GDAL = True gdal.HAS_SRS = True
Copy after login

4. Create a geographical feature model

We need to create some geographic feature models in the application's models.py file to store geographic feature data in the database. For example, we can create a model called "WorldBorder" to store border information of countries in the world. The following is the definition of the model:

# models.py from django.contrib.gis.db import models class WorldBorder(models.Model): name = models.CharField(max_length=50) area = models.IntegerField(default=0) pop2005 = models.IntegerField(default=0) fips = models.CharField(max_length=2) iso2 = models.CharField(max_length=2) iso3 = models.CharField(max_length=3) un = models.IntegerField(default=0) region = models.IntegerField(default=0) subregion = models.IntegerField(default=0) lon = models.FloatField() lat = models.FloatField() mpoly = models.MultiPolygonField() def __str__(self): return self.name
Copy after login

In this model, we define some fields to store basic information of the country/region (such as name, area, population, etc.), and we also define a MultiPolygonField type fields to store boundary information.

5. Create geographical feature data

We need to create some geographical feature data for storage in the database. We can import data into the database through the following command:

ogr2ogr -f "PostgreSQL" PG:"dbname=webgis user=postgres host=127.0.0.1 password=**** port=5432" -nln worldborder -nlt MULTIPOLYGON -update -overwrite -lco GEOMETRY_NAME=mpoly -skipfailures ./world_borders.shp
Copy after login

This command imports the data in the world_borders.shp file into a table named "worldborder".

6. Write view functions

We need to write some view functions in the views.py file of the application in order to respond to user requests. For example, we can write a view function named "map" to display the border information of countries around the world on the map. The following is the definition of the view function:

# views.py from django.shortcuts import render from django.contrib.gis.geos import GEOSGeometry from .models import WorldBorder def map(request): # 获取所有国家/地区 countries = WorldBorder.objects.all() # 构造GeoJSON格式数据 geojson = { "type": "FeatureCollection", "features": [] } for country in countries: feature = { "type": "Feature", "geometry": country.mpoly.geojson, "properties": { "name": country.name, "area": country.area, "pop2005": country.pop2005, "fips": country.fips, "iso2": country.iso2, "iso3": country.iso3, "un": country.un, "region": country.region, "subregion": country.subregion } } geojson["features"].append(feature) # 返回地图页面 return render(request, 'map.html', {'geojson': geojson})
Copy after login

This function first gets the information of all countries/regions and then converts them into GeoJSON format data. Finally, the data is passed to a template named "map.html" for display.

7. Writing templates

We need to create a template named "map.html" in the templates directory of the application to display maps and data. The following is the definition of the template:

    Web GIS Application    
  
Copy after login

This template uses a JavaScript map library called "Leaflet".

8. Run the application

We can execute the following command in the command line to start the Django server:

python manage.py runserver
Copy after login

Then, visit the following address to open the Django server in the browser View Web GIS Applications:

http://127.0.0.1:8000/map
Copy after login

Summary

This article explains how to build a simple Web GIS application using Django and GDAL. By using these tools and techniques, we can easily develop efficient web applications in which to display and analyze geographic data. Additionally, we can use other map libraries and GIS data sources to further extend and optimize our application.

The above is the detailed content of Build Web GIS applications based on Django. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!