[GeoDjango] 02 - Geodjango Series Tutorials

 GeoDjango intends to be a world-class geographic Web framework. Its goal is to make it as easy as possible to build GIS Web applications and harness the power of spatially enabled data.

 

问题来了,其数据库是怎样设计的?

Geodjango Series 1,主要关注其数据相关部分。

 
1.Introduction to Django projects https://youtu.be/L3YoX9wrGDc
2.Spatial data and Geo-databases https://youtu.be/jQlvPAiSCjk
3.Configure Leaflet in Django Admin https://youtu.be/XahUfrU4qk8
4.Generate GIS Model from Geospatial data https://youtu.be/Gtb3QHS2y3g
5.The Frontend (Urls, Views, Templates) https://youtu.be/vWcasu07R7Q
6.Popups in Geodjango https://youtu.be/8InvtB9Vqvs
7.Custom Layer Styles https://youtu.be/d4c-TjxsMNo
9.Routing in Leaflet https://youtu.be/Ci_4r4NcFAk
10.Spatial data editing with QGIS https://youtu.be/1-7KUHLTqjs
11.Geodjango Tutorial Series Summary https://youtu.be/pxX6gI48eh4

代码

The entire application can be found at https://github.com/wanjohikibui/Geodj... 
 
 

一、更换数据库

  • settings.py
DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'agricom',
        'USER': 'postgres',
        'HOST': 'localhost',
        'PASSWORD': '123456',
        'PORT': '5432',
    }
}

 

  • 安装postgresql

Ref: https://www.postgresql.org/

sudo apt-get install postgresql

 

测试一下,看看数据库中的表 有没有?需要创建对应的 database: agricom

(django2) jeff@ThinkPad-T490:django_project$ sudo su postgres

postgres@ThinkPad-T490:/home/jeff/Desktop/django_project$ psql
psql (10.16 (Ubuntu 10.16-0ubuntu0.18.04.1))
Type "help" for help.

postgres=# /database
postgres-# /d

 

  • 安装Psycopg

Ref: https://pypi.org/project/psycopg2/

Psycopg is the most popular PostgreSQL database adapter for the Python programming language. 

pip install psycopg2

 

  • 安装PostGIS

其实安装的是插件:postgresql-10-postgis-2.4

sudo apt-get install postgis

 

  • 扩展插件

  

 

 

 二、增强 models

出现此错误,因为需要migration。 

AttributeError: module 'django.db.models' has no attribute 'PointField'

正确使用方法如下,然后 migration即 可。

from django.db import models
from django.contrib.gis.db import models as gis_models
from django.contrib.gis.geos import Point # GeoDjango takes lat and lng values in Point object.

class Scanner(models.Model):
    user        = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
    name        = models.CharField(max_length=100, null=False, blank=False)
    description = models.TextField(default='default description.')
location
= gis_models.PointField(srid=4326, default=Point(0.0, 0.0)) objects = gis_models.Manager()

 

 

三、增强 map 

继承Leaflet,构成一个新类,再注册到Scanner内。 

from django.contrib import admin
from .models import Scanner, Label
from leaflet.admin import LeafletGeoAdmin


class ScannerAdmin(LeafletGeoAdmin):
    pass

admin.site.register(Scanner, ScannerAdmin)
admin.site.register(Label)

 

 

 

/* continue...*/

 

 

Candidates 

How to integrate Geodjango with Google Maps API 3?

Build a simple GIS web application using GeoDjango and Google Maps

 

posted @ 2021-03-14 06:28  郝壹贰叁  阅读(103)  评论(0)    收藏  举报