1 |
# Copyright (C) 2003 by Intevation GmbH |
# Copyright (C) 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
198 |
run_command(["pg_ctl", "-m", "fast", "-D", self.dbdir, "stop"], |
run_command(["pg_ctl", "-m", "fast", "-D", self.dbdir, "stop"], |
199 |
os.path.join(self.dbdir, "pg_ctl-stop.log")) |
os.path.join(self.dbdir, "pg_ctl-stop.log")) |
200 |
|
|
201 |
def new_postgis_db(self, dbname, tables = None): |
def new_postgis_db(self, dbname, tables = None, reference_systems = None): |
202 |
"""Create and return a new PostGISDatabase object using self as server |
"""Create and return a new PostGISDatabase object using self as server |
203 |
""" |
""" |
204 |
db = PostGISDatabase(self, self.postgis_sql, dbname, tables = tables) |
db = PostGISDatabase(self, self.postgis_sql, dbname, tables = tables, |
205 |
|
reference_systems = reference_systems) |
206 |
db.initdb() |
db.initdb() |
207 |
self.known_dbs[dbname] = db |
self.known_dbs[dbname] = db |
208 |
return db |
return db |
209 |
|
|
210 |
def get_static_data_db(self, dbname, tables): |
def get_static_data_db(self, dbname, tables, reference_systems): |
211 |
"""Return a PostGISDatabase for a database with the given static data |
"""Return a PostGISDatabase for a database with the given static data |
212 |
|
|
213 |
If no databasse of the name dbname exists, create a new one via |
If no databasse of the name dbname exists, create a new one via |
217 |
indicated data, return that. If the already existing db uses |
indicated data, return that. If the already existing db uses |
218 |
different data raise a value error. |
different data raise a value error. |
219 |
|
|
220 |
The tables argument should be a sequence of table specifications |
If the database doesn't exist, create a new one via |
221 |
where each specifications is a (tablename, shapefilename) pair. |
self.new_postgis_db. |
222 |
|
|
223 |
|
The parameters tables and reference_systems have the same |
224 |
|
meaning as for new_postgis_db. |
225 |
""" |
""" |
226 |
db = self.known_dbs.get(dbname) |
db = self.known_dbs.get(dbname) |
227 |
if db is not None: |
if db is not None: |
228 |
if db.has_data(tables): |
if db.has_data(tables, reference_systems): |
229 |
return db |
return db |
230 |
raise ValueError("PostGISDatabase named %r doesn't have tables %r" |
raise ValueError("PostGISDatabase named %r doesn't have tables %r" |
231 |
% (dbname, tables)) |
% (dbname, tables)) |
232 |
return self.new_postgis_db(dbname, tables) |
return self.new_postgis_db(dbname, tables, reference_systems) |
233 |
|
|
234 |
def get_default_static_data_db(self): |
def get_default_static_data_db(self): |
235 |
dbname = "PostGISStaticTests" |
dbname = "PostGISStaticTests" |
236 |
|
srids = [(1, "proj=longlat datum=WGS84")] |
237 |
tables = [ |
tables = [ |
238 |
# Direct copies of the shapefiles. The shapeids are exactly |
# Direct copies of the shapefiles. The shapeids are exactly |
239 |
# the same. |
# the same, except where changed with "gid_offset", of |
240 |
|
# course |
241 |
("landmarks", os.path.join("..", "Data", "iceland", |
("landmarks", os.path.join("..", "Data", "iceland", |
242 |
"cultural_landmark-point.shp"), |
"cultural_landmark-point.shp"), |
243 |
[("gid_offset", 1000)]), |
[("gid_offset", 1000)]), |
250 |
("political_multi", os.path.join("..", "Data", "iceland", |
("political_multi", os.path.join("..", "Data", "iceland", |
251 |
"political.shp"), |
"political.shp"), |
252 |
[("force_wkt_type", "MULTIPOLYGON")]), |
[("force_wkt_type", "MULTIPOLYGON")]), |
253 |
|
|
254 |
|
# Copy of landmarks but using an srid |
255 |
|
("landmarks_srid", os.path.join("..", "Data", "iceland", |
256 |
|
"cultural_landmark-point.shp"), |
257 |
|
[("gid_offset", 1000), |
258 |
|
("srid", 1)]), |
259 |
] |
] |
260 |
return self.get_static_data_db(dbname, tables) |
return self.get_static_data_db(dbname, tables, srids) |
261 |
|
|
262 |
def connection_params(self, user): |
def connection_params(self, user): |
263 |
"""Return the connection parameters for the given user |
"""Return the connection parameters for the given user |
335 |
|
|
336 |
"""A PostGIS database in a PostgreSQLServer""" |
"""A PostGIS database in a PostgreSQLServer""" |
337 |
|
|
338 |
def __init__(self, server, postgis_sql, dbname, tables = None): |
def __init__(self, server, postgis_sql, dbname, tables = None, |
339 |
|
reference_systems = ()): |
340 |
"""Initialize the PostGISDatabase |
"""Initialize the PostGISDatabase |
341 |
|
|
342 |
Parameters: |
Parameters: |
356 |
shapefile or (tablename, shapefilename, extraargs) |
shapefile or (tablename, shapefilename, extraargs) |
357 |
triples. The extraargs should be a list of key, value |
triples. The extraargs should be a list of key, value |
358 |
pairs to use as keyword arguments to upload_shapefile. |
pairs to use as keyword arguments to upload_shapefile. |
359 |
|
|
360 |
|
reference_systems -- Optional description of spatial |
361 |
|
reference systems. If given, it should be a sequence of |
362 |
|
(srid, params) pairs where srid is the srid defined by |
363 |
|
the proj4 paramter string params. The srid can be given |
364 |
|
as an extra parameter in the tables list. |
365 |
""" |
""" |
366 |
self.server = server |
self.server = server |
367 |
self.postgis_sql = postgis_sql |
self.postgis_sql = postgis_sql |
368 |
self.dbname = dbname |
self.dbname = dbname |
369 |
self.tables = tables |
self.tables = tables |
370 |
|
if reference_systems: |
371 |
|
self.reference_systems = reference_systems |
372 |
|
else: |
373 |
|
# Make sure that it's a sequence we can iterate over even if |
374 |
|
# the parameter's None |
375 |
|
self.reference_systems = () |
376 |
|
|
377 |
def initdb(self): |
def initdb(self): |
378 |
"""Remove the old db directory and create and initialize a new database |
"""Remove the old db directory and create and initialize a new database |
398 |
|
|
399 |
self.server.execute_sql(self.dbname, "admin", |
self.server.execute_sql(self.dbname, "admin", |
400 |
"GRANT SELECT ON geometry_columns TO PUBLIC;") |
"GRANT SELECT ON geometry_columns TO PUBLIC;") |
401 |
|
self.server.execute_sql(self.dbname, "admin", |
402 |
|
"GRANT SELECT ON spatial_ref_sys TO PUBLIC;") |
403 |
|
|
404 |
|
for srid, params in self.reference_systems: |
405 |
|
self.server.execute_sql(self.dbname, "admin", |
406 |
|
"INSERT INTO spatial_ref_sys VALUES" |
407 |
|
" (%d, '', %d, '', '%s');" |
408 |
|
% (srid, srid, params)) |
409 |
if self.tables is not None: |
if self.tables is not None: |
410 |
def unpack(item): |
def unpack(item): |
411 |
extra = {"force_wkt_type": None, "gid_offset": 0} |
extra = {"force_wkt_type": None, "gid_offset": 0, |
412 |
|
"srid": -1} |
413 |
if len(info) == 2: |
if len(info) == 2: |
414 |
tablename, shapefile = info |
tablename, shapefile = info |
415 |
else: |
else: |
422 |
tablename, shapefile, kw = unpack(info) |
tablename, shapefile, kw = unpack(info) |
423 |
upload_shapefile(shapefile, self, tablename, **kw) |
upload_shapefile(shapefile, self, tablename, **kw) |
424 |
|
|
425 |
def has_data(self, tables): |
def has_data(self, tables, reference_systems): |
426 |
return self.tables == tables |
return (self.tables == tables |
427 |
|
and self.reference_systems == reference_systems) |
428 |
|
|
429 |
|
|
430 |
def find_postgis_sql(): |
def find_postgis_sql(): |
573 |
} |
} |
574 |
|
|
575 |
def upload_shapefile(filename, db, tablename, force_wkt_type = None, |
def upload_shapefile(filename, db, tablename, force_wkt_type = None, |
576 |
gid_offset = 0): |
gid_offset = 0, srid = -1): |
577 |
"""Upload a shapefile into a new database table |
"""Upload a shapefile into a new database table |
578 |
|
|
579 |
Parameters: |
Parameters: |
591 |
|
|
592 |
gid_offset -- A number to add to the shapeid to get the value for |
gid_offset -- A number to add to the shapeid to get the value for |
593 |
the gid column (default 0) |
the gid column (default 0) |
594 |
|
|
595 |
|
srid -- The srid of the spatial references system used by the table |
596 |
|
and the data |
597 |
""" |
""" |
598 |
import dbflib, shapelib |
import dbflib, shapelib |
599 |
|
|
636 |
convert = wkt_converter[wkttype] |
convert = wkt_converter[wkttype] |
637 |
|
|
638 |
cursor.execute("select AddGeometryColumn('%(dbname)s'," |
cursor.execute("select AddGeometryColumn('%(dbname)s'," |
639 |
"'%(tablename)s', 'the_geom', '-1', '%(wkttype)s', 2);" |
"'%(tablename)s', 'the_geom', %(srid)d, '%(wkttype)s', 2);" |
640 |
% locals()) |
% locals()) |
641 |
|
|
642 |
insert_formats.append("GeometryFromText(%(the_geom)s, -1)") |
insert_formats.append("GeometryFromText(%(the_geom)s, %(srid)d)") |
643 |
|
|
644 |
insert = ("INSERT INTO %s VALUES (%s)" |
insert = ("INSERT INTO %s VALUES (%s)" |
645 |
% (tablename, ", ".join(insert_formats))) |
% (tablename, ", ".join(insert_formats))) |
648 |
data = dbf.read_record(i) |
data = dbf.read_record(i) |
649 |
data["tablename"] = tablename |
data["tablename"] = tablename |
650 |
data["gid"] = i + gid_offset |
data["gid"] = i + gid_offset |
651 |
|
data["srid"] = srid |
652 |
data["the_geom"] = convert(shp.read_object(i).vertices()) |
data["the_geom"] = convert(shp.read_object(i).vertices()) |
653 |
#print insert % data |
#print insert % data |
654 |
cursor.execute(insert, data) |
cursor.execute(insert, data) |