Database Reference
In-Depth Information
= 10USERNAME = 'postgis' #enter your
username heredef CreatePGLayer():"""
Create the PostGIS table."""driver =
ogr.GetDriverByName('PostgreSQL')srs =
osr.SpatialReference()srs.ImportFromEPSG(4326)pg_ds
= ogr.Open("PG:dbname='postgis_cookbook'
host='localhost' port='5432' user='me'
password='mypassword'",update = 1
)pg_layer =
pg_ds.CreateLayer('wikiplaces', srs =
srs, geom_type=ogr.wkbPoint,options =
['DIM=3', # we want to store the
elevation value in point z
coordinate'GEOMETRY_NAME=the_geom','OVERWRITE=YES',
# this will drop and recreate the table
every time'SCHEMA=chp08',])# add the
fieldsfd_title = ogr.FieldDefn('title',
ogr.OFTString)pg_layer.CreateField(fd_title)fd_countrycode
= ogr.FieldDefn('countrycode',
ogr.OFTString)pg_layer.CreateField(fd_countrycode)fd_feature
= ogr.FieldDefn('feature',
ogr.OFTString)pg_layer.CreateField(fd_feature)fd_thumbnail
= ogr.FieldDefn('thumbnail',
ogr.OFTString)pg_layer.CreateField(fd_thumbnail)fd_wikipediaurl
= ogr.FieldDefn('wikipediaurl',
ogr.OFTString)pg_layer.CreateField(fd_wikipediaurl)return
pg_ds, pg_layerdef
AddPlacesToLayer(places):"""Read the
places dictionary list and add features
in the PostGIS table for each place."""#
iterate every place dictionary in the
listfor place in places:lng =
place['lng']lat = place['lat']z =
place['elevation'] if 'elevation' in
place else 0# we generate a point
representation in wkt, and create an ogr
geometrypoint_wkt = 'POINT(%s %s %s)' %
(lng, lat, z)point =
Search WWH ::




Custom Search