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, except where changed with "gid_offset", of |
# the same, except where changed with "gid_offset", of |
240 |
# course |
# course. Note that the test implementation requires that |
241 |
|
# all the landmard tables use an gid_offset of 1000. |
242 |
("landmarks", os.path.join("..", "Data", "iceland", |
("landmarks", os.path.join("..", "Data", "iceland", |
243 |
"cultural_landmark-point.shp"), |
"cultural_landmark-point.shp"), |
244 |
[("gid_offset", 1000)]), |
[("gid_offset", 1000)]), |
252 |
"political.shp"), |
"political.shp"), |
253 |
[("force_wkt_type", "MULTIPOLYGON")]), |
[("force_wkt_type", "MULTIPOLYGON")]), |
254 |
|
|
255 |
# Copy of landmarks but using an srid |
# Copy of landmarks but using an srid != -1 |
256 |
("landmarks_srid", os.path.join("..", "Data", "iceland", |
("landmarks_srid", os.path.join("..", "Data", "iceland", |
257 |
"cultural_landmark-point.shp"), |
"cultural_landmark-point.shp"), |
258 |
[("gid_offset", 1000), |
[("gid_offset", 1000), |
259 |
("srid", 1)]), |
("srid", 1)]), |
260 |
|
|
261 |
|
# Copy of landmarks with a gid column called "point_id" instead |
262 |
|
# of "gid" and using an srid != -1. |
263 |
|
("landmarks_point_id", os.path.join("..", "Data", "iceland", |
264 |
|
"cultural_landmark-point.shp"), |
265 |
|
[("gid_offset", 1000), |
266 |
|
("srid", 1), |
267 |
|
("gid_column", "point_id")]), |
268 |
] |
] |
269 |
return self.get_static_data_db(dbname, tables, srids) |
return self.get_static_data_db(dbname, tables, srids) |
270 |
|
|
538 |
|
|
539 |
The test performed by this function is a bit simplistic because it |
The test performed by this function is a bit simplistic because it |
540 |
only tests whether the string 'quote_ident' occurs anywhere in the |
only tests whether the string 'quote_ident' occurs anywhere in the |
541 |
postgis.sql file. This will hopefully works because when this was |
postgis.sql file. This will hopefully work because when this was |
542 |
fixed in postgis CVS AddGeometryColumn was the first function to use |
fixed in postgis CVS AddGeometryColumn was the first function to use |
543 |
quote_ident. |
quote_ident. |
544 |
""" |
""" |
582 |
} |
} |
583 |
|
|
584 |
def upload_shapefile(filename, db, tablename, force_wkt_type = None, |
def upload_shapefile(filename, db, tablename, force_wkt_type = None, |
585 |
gid_offset = 0, srid = -1): |
gid_offset = 0, gid_column = "gid", srid = -1): |
586 |
"""Upload a shapefile into a new database table |
"""Upload a shapefile into a new database table |
587 |
|
|
588 |
Parameters: |
Parameters: |
601 |
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 |
602 |
the gid column (default 0) |
the gid column (default 0) |
603 |
|
|
604 |
|
gid_column -- The name of the column with the shape ids. Default |
605 |
|
'gid'. If None, no gid column will be created. The |
606 |
|
name is directly used in SQL statements, so if it |
607 |
|
contains unusualy characters the caller should provide |
608 |
|
a suitable quoted string. |
609 |
|
|
610 |
srid -- The srid of the spatial references system used by the table |
srid -- The srid of the spatial references system used by the table |
611 |
and the data |
and the data |
612 |
""" |
""" |
633 |
dbflib.FTInteger: "INTEGER", |
dbflib.FTInteger: "INTEGER", |
634 |
dbflib.FTDouble: "DOUBLE PRECISION"} |
dbflib.FTDouble: "DOUBLE PRECISION"} |
635 |
|
|
636 |
insert_formats = ["%(gid)s"] |
insert_formats = [] |
637 |
fields = ["gid INT"] |
if gid_column: |
638 |
|
insert_formats.append("%(gid)s") |
639 |
|
|
640 |
|
fields = [] |
641 |
|
fields_decl = [] |
642 |
|
if gid_column: |
643 |
|
fields.append(gid_column) |
644 |
|
fields_decl.append("%s INT" % gid_column) |
645 |
for i in range(dbf.field_count()): |
for i in range(dbf.field_count()): |
646 |
ftype, name, width, prec = dbf.field_info(i) |
ftype, name, width, prec = dbf.field_info(i) |
647 |
fields.append("%s %s" % (name, typemap[ftype])) |
fields.append(name) |
648 |
|
fields_decl.append("%s %s" % (name, typemap[ftype])) |
649 |
insert_formats.append("%%(%s)s" % name) |
insert_formats.append("%%(%s)s" % name) |
650 |
stmt = "CREATE TABLE %s (\n %s\n);" % (tablename, |
stmt = "CREATE TABLE %s (\n %s\n);" % (tablename, |
651 |
",\n ".join(fields)) |
",\n ".join(fields_decl)) |
652 |
cursor.execute(stmt) |
cursor.execute(stmt) |
653 |
#print stmt |
#print stmt |
654 |
|
|
661 |
cursor.execute("select AddGeometryColumn('%(dbname)s'," |
cursor.execute("select AddGeometryColumn('%(dbname)s'," |
662 |
"'%(tablename)s', 'the_geom', %(srid)d, '%(wkttype)s', 2);" |
"'%(tablename)s', 'the_geom', %(srid)d, '%(wkttype)s', 2);" |
663 |
% locals()) |
% locals()) |
664 |
|
fields.append("the_geom") |
665 |
insert_formats.append("GeometryFromText(%(the_geom)s, %(srid)d)") |
insert_formats.append("GeometryFromText(%(the_geom)s, %(srid)d)") |
666 |
|
|
667 |
insert = ("INSERT INTO %s VALUES (%s)" |
insert = ("INSERT INTO %s (%s) VALUES (%s)" |
668 |
% (tablename, ", ".join(insert_formats))) |
% (tablename, ", ".join(fields), ", ".join(insert_formats))) |
669 |
|
|
670 |
for i in range(numshapes): |
for i in range(numshapes): |
671 |
data = dbf.read_record(i) |
data = dbf.read_record(i) |
672 |
data["tablename"] = tablename |
data["tablename"] = tablename |
673 |
data["gid"] = i + gid_offset |
if gid_column: |
674 |
|
data["gid"] = i + gid_offset |
675 |
data["srid"] = srid |
data["srid"] = srid |
676 |
data["the_geom"] = convert(shp.read_object(i).vertices()) |
data["the_geom"] = convert(shp.read_object(i).vertices()) |
677 |
#print insert % data |
#print insert % data |