load gis 数据

1.根据 .prj(坐标系文件) 文件在db 里面创建 srid

INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values ( 97688, 'sr-org', 7688,
'+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +units=m +no_defs',
'PROJCS["WGS_1984_Web_Mercator",GEOGCS["GCS_WGS_1984_Major_Auxiliary_Sphere",DATUM["D_WGS_1984_Major_Auxiliary_Sphere",SPHEROID["WGS_1984_Major_Auxiliary_Sphere",6378137.0,0.0]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],UNIT["Meter",1.0]]');

2.使用 .shp 文件 生成 model 以及 mapping

python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorder --srid=97688 --mapping --multi

关于上面给出的命令行选项的几点注释:
--srid=97688 选项设置地理字段的 srid。
--mapping 选项指示 ogrinspect 也生成用于 LayerMapping 的映射字典。
--multi选项,以使地理字段为 MultiPolygonField。
生成 models 和 mapping 如下:

from django.contrib.gis.db import models

class WorldBorder(models.Model):
  fips = models.CharField(max_length=2)
  iso2 = models.CharField(max_length=2)
  iso3 = models.CharField(max_length=3)
  un = models.IntegerField()
  name = models.CharField(max_length=50)
  area = models.IntegerField()
  pop2005 = models.BigIntegerField()
  region = models.IntegerField()
  subregion = models.IntegerField()
  lon = models.FloatField()
  lat = models.FloatField()
  geom = models.MultiPolygonField(srid=97688)

# Auto-generated `LayerMapping` dictionary for WorldBorder model
worldborder_mapping = {
  'fips' : 'FIPS',
  'iso2' : 'ISO2',
  'iso3' : 'ISO3',
  'un' : 'UN',
  'name' : 'NAME',
  'area' : 'AREA',
  'pop2005' : 'POP2005',
  'region' : 'REGION',
  'subregion' : 'SUBREGION',
  'lon' : 'LON',
  'lat' : 'LAT',
  'geom' : 'MULTIPOLYGON',
}

3.根据 model 创建 db 表
4.根据 mapping 使用 layermapping load 数据到 db

import os
from django.contrib.gis.utils import LayerMapping
from .models import WorldBorder

world_mapping = {
    'fips' : 'FIPS',
    'iso2' : 'ISO2',
    'iso3' : 'ISO3',
    'un' : 'UN',
    'name' : 'NAME',
    'area' : 'AREA',
    'pop2005' : 'POP2005',
    'region' : 'REGION',
    'subregion' : 'SUBREGION',
    'lon' : 'LON',
    'lat' : 'LAT',
    'mpoly' : 'MULTIPOLYGON',
}

world_shp = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/TM_WORLD_BORDERS-0.3.shp'))

def run(verbose=True):
    lm = LayerMapping(WorldBorder, world_shp, world_mapping,
                      transform=False, encoding='iso-8859-1')

    lm.save(strict=True, verbose=verbose)

5.使用 ST_Transform 在各坐标系间转换

SELECT ST_AsText(ST_Transform(mpoly,4326)) As wgs_geom from world_worldborder'

你可能感兴趣的:(load gis 数据)