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, reference_systems = None): |
def new_postgis_db(self, dbname, tables = None, reference_systems = None, |
202 |
|
views = None): |
203 |
"""Create and return a new PostGISDatabase object using self as server |
"""Create and return a new PostGISDatabase object using self as server |
204 |
""" |
""" |
205 |
db = PostGISDatabase(self, self.postgis_sql, dbname, tables = tables, |
db = PostGISDatabase(self, self.postgis_sql, dbname, tables = tables, |
206 |
reference_systems = reference_systems) |
reference_systems = reference_systems, |
207 |
|
views = views) |
208 |
db.initdb() |
db.initdb() |
209 |
self.known_dbs[dbname] = db |
self.known_dbs[dbname] = db |
210 |
return db |
return db |
211 |
|
|
212 |
def get_static_data_db(self, dbname, tables, reference_systems): |
def get_static_data_db(self, dbname, tables, reference_systems, views): |
213 |
"""Return a PostGISDatabase for a database with the given static data |
"""Return a PostGISDatabase for a database with the given static data |
214 |
|
|
215 |
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 |
227 |
""" |
""" |
228 |
db = self.known_dbs.get(dbname) |
db = self.known_dbs.get(dbname) |
229 |
if db is not None: |
if db is not None: |
230 |
if db.has_data(tables, reference_systems): |
if db.has_data(tables, reference_systems, views): |
231 |
return db |
return db |
232 |
raise ValueError("PostGISDatabase named %r doesn't have tables %r" |
raise ValueError("PostGISDatabase named %r doesn't have tables %r" |
233 |
% (dbname, tables)) |
% (dbname, tables)) |
234 |
return self.new_postgis_db(dbname, tables, reference_systems) |
return self.new_postgis_db(dbname, tables, reference_systems, views) |
235 |
|
|
236 |
def get_default_static_data_db(self): |
def get_default_static_data_db(self): |
237 |
dbname = "PostGISStaticTests" |
dbname = "PostGISStaticTests" |
239 |
tables = [ |
tables = [ |
240 |
# Direct copies of the shapefiles. The shapeids are exactly |
# Direct copies of the shapefiles. The shapeids are exactly |
241 |
# the same, except where changed with "gid_offset", of |
# the same, except where changed with "gid_offset", of |
242 |
# course |
# course. Note that the test implementation requires that |
243 |
|
# all the landmard tables use an gid_offset of 1000. |
244 |
("landmarks", os.path.join("..", "Data", "iceland", |
("landmarks", os.path.join("..", "Data", "iceland", |
245 |
"cultural_landmark-point.shp"), |
"cultural_landmark-point.shp"), |
246 |
[("gid_offset", 1000)]), |
[("gid_offset", 1000)]), |
254 |
"political.shp"), |
"political.shp"), |
255 |
[("force_wkt_type", "MULTIPOLYGON")]), |
[("force_wkt_type", "MULTIPOLYGON")]), |
256 |
|
|
257 |
# Copy of landmarks but using an srid |
# Copy of landmarks but using an srid != -1 |
258 |
("landmarks_srid", os.path.join("..", "Data", "iceland", |
("landmarks_srid", os.path.join("..", "Data", "iceland", |
259 |
"cultural_landmark-point.shp"), |
"cultural_landmark-point.shp"), |
260 |
[("gid_offset", 1000), |
[("gid_offset", 1000), |
261 |
("srid", 1)]), |
("srid", 1)]), |
262 |
|
|
263 |
|
# Copy of landmarks with a gid column called "point_id" instead |
264 |
|
# of "gid" and using an srid != -1. |
265 |
|
("landmarks_point_id", os.path.join("..", "Data", "iceland", |
266 |
|
"cultural_landmark-point.shp"), |
267 |
|
[("gid_offset", 1000), |
268 |
|
("srid", 1), |
269 |
|
("gid_column", "point_id")]), |
270 |
] |
] |
271 |
return self.get_static_data_db(dbname, tables, srids) |
views = [("v_landmarks", "SELECT * FROM landmarks_point_id")] |
272 |
|
return self.get_static_data_db(dbname, tables, srids, views) |
273 |
|
|
274 |
def connection_params(self, user): |
def connection_params(self, user): |
275 |
"""Return the connection parameters for the given user |
"""Return the connection parameters for the given user |
348 |
"""A PostGIS database in a PostgreSQLServer""" |
"""A PostGIS database in a PostgreSQLServer""" |
349 |
|
|
350 |
def __init__(self, server, postgis_sql, dbname, tables = None, |
def __init__(self, server, postgis_sql, dbname, tables = None, |
351 |
reference_systems = ()): |
reference_systems = (), views = None): |
352 |
"""Initialize the PostGISDatabase |
"""Initialize the PostGISDatabase |
353 |
|
|
354 |
Parameters: |
Parameters: |
374 |
(srid, params) pairs where srid is the srid defined by |
(srid, params) pairs where srid is the srid defined by |
375 |
the proj4 paramter string params. The srid can be given |
the proj4 paramter string params. The srid can be given |
376 |
as an extra parameter in the tables list. |
as an extra parameter in the tables list. |
377 |
|
|
378 |
|
views -- Optional description of views. If given it should |
379 |
|
be a list of (viewname, select_stmt) pairs where |
380 |
|
viewname is the name of the view to be created and |
381 |
|
select_stmt is the select statement to use as the basis. |
382 |
|
The views will be created after the tables and may refer |
383 |
|
to them in the select_stmt. |
384 |
""" |
""" |
385 |
self.server = server |
self.server = server |
386 |
self.postgis_sql = postgis_sql |
self.postgis_sql = postgis_sql |
387 |
self.dbname = dbname |
self.dbname = dbname |
388 |
self.tables = tables |
self.tables = tables |
389 |
|
self.views = views |
390 |
if reference_systems: |
if reference_systems: |
391 |
self.reference_systems = reference_systems |
self.reference_systems = reference_systems |
392 |
else: |
else: |
442 |
tablename, shapefile, kw = unpack(info) |
tablename, shapefile, kw = unpack(info) |
443 |
upload_shapefile(shapefile, self, tablename, **kw) |
upload_shapefile(shapefile, self, tablename, **kw) |
444 |
|
|
445 |
def has_data(self, tables, reference_systems): |
if self.views is not None: |
446 |
|
for viewname, select_stmt in self.views: |
447 |
|
self.server.execute_sql(self.dbname, "admin", |
448 |
|
"CREATE VIEW %s AS %s" % (viewname, |
449 |
|
select_stmt)) |
450 |
|
self.server.execute_sql(self.dbname, "admin", |
451 |
|
"GRANT SELECT ON %s TO PUBLIC;" |
452 |
|
% viewname) |
453 |
|
|
454 |
|
def has_data(self, tables, reference_systems, views): |
455 |
return (self.tables == tables |
return (self.tables == tables |
456 |
and self.reference_systems == reference_systems) |
and self.reference_systems == reference_systems |
457 |
|
and self.views == views) |
458 |
|
|
459 |
|
|
460 |
def find_postgis_sql(): |
def find_postgis_sql(): |
559 |
|
|
560 |
The test performed by this function is a bit simplistic because it |
The test performed by this function is a bit simplistic because it |
561 |
only tests whether the string 'quote_ident' occurs anywhere in the |
only tests whether the string 'quote_ident' occurs anywhere in the |
562 |
postgis.sql file. This will hopefully works because when this was |
postgis.sql file. This will hopefully work because when this was |
563 |
fixed in postgis CVS AddGeometryColumn was the first function to use |
fixed in postgis CVS AddGeometryColumn was the first function to use |
564 |
quote_ident. |
quote_ident. |
565 |
""" |
""" |
603 |
} |
} |
604 |
|
|
605 |
def upload_shapefile(filename, db, tablename, force_wkt_type = None, |
def upload_shapefile(filename, db, tablename, force_wkt_type = None, |
606 |
gid_offset = 0, srid = -1): |
gid_offset = 0, gid_column = "gid", srid = -1): |
607 |
"""Upload a shapefile into a new database table |
"""Upload a shapefile into a new database table |
608 |
|
|
609 |
Parameters: |
Parameters: |
622 |
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 |
623 |
the gid column (default 0) |
the gid column (default 0) |
624 |
|
|
625 |
|
gid_column -- The name of the column with the shape ids. Default |
626 |
|
'gid'. If None, no gid column will be created. The |
627 |
|
name is directly used in SQL statements, so if it |
628 |
|
contains unusualy characters the caller should provide |
629 |
|
a suitable quoted string. |
630 |
|
|
631 |
srid -- The srid of the spatial references system used by the table |
srid -- The srid of the spatial references system used by the table |
632 |
and the data |
and the data |
633 |
""" |
""" |
654 |
dbflib.FTInteger: "INTEGER", |
dbflib.FTInteger: "INTEGER", |
655 |
dbflib.FTDouble: "DOUBLE PRECISION"} |
dbflib.FTDouble: "DOUBLE PRECISION"} |
656 |
|
|
657 |
insert_formats = ["%(gid)s"] |
insert_formats = [] |
658 |
fields = ["gid INT"] |
if gid_column: |
659 |
|
insert_formats.append("%(gid)s") |
660 |
|
|
661 |
|
fields = [] |
662 |
|
fields_decl = [] |
663 |
|
if gid_column: |
664 |
|
fields.append(gid_column) |
665 |
|
fields_decl.append("%s INT" % gid_column) |
666 |
for i in range(dbf.field_count()): |
for i in range(dbf.field_count()): |
667 |
ftype, name, width, prec = dbf.field_info(i) |
ftype, name, width, prec = dbf.field_info(i) |
668 |
fields.append("%s %s" % (name, typemap[ftype])) |
fields.append(name) |
669 |
|
fields_decl.append("%s %s" % (name, typemap[ftype])) |
670 |
insert_formats.append("%%(%s)s" % name) |
insert_formats.append("%%(%s)s" % name) |
671 |
stmt = "CREATE TABLE %s (\n %s\n);" % (tablename, |
stmt = "CREATE TABLE %s (\n %s\n);" % (tablename, |
672 |
",\n ".join(fields)) |
",\n ".join(fields_decl)) |
673 |
cursor.execute(stmt) |
cursor.execute(stmt) |
674 |
#print stmt |
#print stmt |
675 |
|
|
682 |
cursor.execute("select AddGeometryColumn('%(dbname)s'," |
cursor.execute("select AddGeometryColumn('%(dbname)s'," |
683 |
"'%(tablename)s', 'the_geom', %(srid)d, '%(wkttype)s', 2);" |
"'%(tablename)s', 'the_geom', %(srid)d, '%(wkttype)s', 2);" |
684 |
% locals()) |
% locals()) |
685 |
|
fields.append("the_geom") |
686 |
insert_formats.append("GeometryFromText(%(the_geom)s, %(srid)d)") |
insert_formats.append("GeometryFromText(%(the_geom)s, %(srid)d)") |
687 |
|
|
688 |
insert = ("INSERT INTO %s VALUES (%s)" |
insert = ("INSERT INTO %s (%s) VALUES (%s)" |
689 |
% (tablename, ", ".join(insert_formats))) |
% (tablename, ", ".join(fields), ", ".join(insert_formats))) |
690 |
|
|
691 |
for i in range(numshapes): |
for i in range(numshapes): |
692 |
data = dbf.read_record(i) |
data = dbf.read_record(i) |
693 |
data["tablename"] = tablename |
data["tablename"] = tablename |
694 |
data["gid"] = i + gid_offset |
if gid_column: |
695 |
|
data["gid"] = i + gid_offset |
696 |
data["srid"] = srid |
data["srid"] = srid |
697 |
data["the_geom"] = convert(shp.read_object(i).vertices()) |
data["the_geom"] = convert(shp.read_object(i).vertices()) |
698 |
#print insert % data |
#print insert % data |