/[thuban]/branches/WIP-pyshapelib-bramz/test/postgissupport.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/test/postgissupport.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2057 by bh, Tue Feb 10 15:51:57 2004 UTC revision 2096 by bh, Thu Mar 11 13:50:53 2004 UTC
# Line 237  class PostgreSQLServer: Line 237  class PostgreSQLServer:
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)]),
# Line 251  class PostgreSQLServer: Line 252  class PostgreSQLServer:
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    
# Line 529  def skip_if_addgeometrycolumn_does_not_u Line 538  def skip_if_addgeometrycolumn_does_not_u
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      """      """
# Line 573  wkt_converter = { Line 582  wkt_converter = {
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:
# Line 592  def upload_shapefile(filename, db, table Line 601  def upload_shapefile(filename, db, table
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      """      """
# Line 618  def upload_shapefile(filename, db, table Line 633  def upload_shapefile(filename, db, table
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    
# Line 638  def upload_shapefile(filename, db, table Line 661  def upload_shapefile(filename, db, table
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

Legend:
Removed from v.2057  
changed lines
  Added in v.2096

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26