/[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 1634 by bh, Fri Aug 22 16:55:19 2003 UTC revision 1656 by bh, Mon Aug 25 18:26:54 2003 UTC
# Line 233  class PostgreSQLServer: Line 233  class PostgreSQLServer:
233                                               "cultural_landmark-point.shp")),                                               "cultural_landmark-point.shp")),
234                    ("political", os.path.join("..", "Data", "iceland",                    ("political", os.path.join("..", "Data", "iceland",
235                                               "political.shp")),                                               "political.shp")),
236    
237                      # The polygon data as a MULTIPOLYGON geometry type
238                      ("political_multi", os.path.join("..", "Data", "iceland",
239                                                 "political.shp"),
240                       "MULTIPOLYGON"),
241    
242                    ("roads", os.path.join("..", "Data", "iceland",                    ("roads", os.path.join("..", "Data", "iceland",
243                                           "roads-line.shp"))]                                           "roads-line.shp"))]
244          return self.get_static_data_db(dbname, tables)          return self.get_static_data_db(dbname, tables)
# Line 345  class PostGISDatabase: Line 351  class PostGISDatabase:
351                                  "GRANT SELECT ON geometry_columns TO PUBLIC;")                                  "GRANT SELECT ON geometry_columns TO PUBLIC;")
352    
353          if self.tables is not None:          if self.tables is not None:
354              for tablename, shapefile in self.tables:              for info  in self.tables:
355                  upload_shapefile(shapefile, self, tablename)                  if len(info) == 2:
356                        tablename, shapefile = info
357                        wkt_type = None
358                    else:
359                        tablename, shapefile, wkt_type = info
360                    upload_shapefile(shapefile, self, tablename,
361                                     force_wkt_type = wkt_type)
362    
363      def has_data(self, tables):      def has_data(self, tables):
364          return self.tables == tables          return self.tables == tables
# Line 440  def skip_if_no_postgis(): Line 452  def skip_if_no_postgis():
452      if _cannot_run_postgis_tests:      if _cannot_run_postgis_tests:
453          raise support.SkipTest(_cannot_run_postgis_tests)          raise support.SkipTest(_cannot_run_postgis_tests)
454    
455  def point_to_wkt(coords):  def coords_to_point(coords):
456      """Return string with a WKT representation of the point in coords"""      """Return string with a WKT representation of the point in coords"""
457      x, y = coords[0]      x, y = coords[0]
458      return "POINT(%r %r)" % (x, y)      return "POINT(%r %r)" % (x, y)
459    
460  def polygon_to_wkt(coords):  def coords_to_polygon(coords):
461      """Return string with a WKT representation of the polygon in coords"""      """Return string with a WKT representation of the polygon in coords"""
462      poly = []      poly = []
463      for ring in coords:      for ring in coords:
464          poly.append(", ".join(["%r %r" % p for p in ring]))          poly.append(", ".join(["%r %r" % p for p in ring]))
465      return "POLYGON((%s))" % "), (".join(poly)      return "POLYGON((%s))" % "), (".join(poly)
466    
467  def arc_to_wkt(coords):  def coords_to_multilinestring(coords):
468      """Return string with a WKT representation of the arc in coords"""      """Return string with a WKT representation of the arc in coords"""
469      poly = []      poly = []
470      for ring in coords:      for ring in coords:
471          poly.append(", ".join(["%r %r" % p for p in ring]))          poly.append(", ".join(["%r %r" % p for p in ring]))
472      return "MULTILINESTRING((%s))" % "), (".join(poly)      return "MULTILINESTRING((%s))" % "), (".join(poly)
473    
474  def upload_shapefile(filename, db, tablename):  def coords_to_multipolygon(coords):
475        """Return string with a WKT representation of the polygon in coords"""
476        poly = []
477        for ring in coords:
478            poly.append(", ".join(["%r %r" % p for p in ring]))
479        return "MULTIPOLYGON(((%s)))" % ")), ((".join(poly)
480    
481    wkt_converter = {
482        "POINT": coords_to_point,
483        "MULTILINESTRING": coords_to_multilinestring,
484        "POLYGON": coords_to_polygon,
485        "MULTIPOLYGON": coords_to_multipolygon,
486        }
487    
488    def upload_shapefile(filename, db, tablename, force_wkt_type = None):
489      import dbflib, shapelib      import dbflib, shapelib
490    
491        # We build this map here because we need shapelib which can only be
492        # imported after support.initthuban has been called which we can't
493        # easily do in this module because it's imported by support.
494        shp_to_wkt = {
495            shapelib.SHPT_POINT: "POINT",
496            shapelib.SHPT_ARC: "MULTILINESTRING",
497            shapelib.SHPT_POLYGON: "POLYGON",
498            }
499    
500      server = db.server      server = db.server
501      dbname = db.dbname      dbname = db.dbname
502      conn = psycopg.connect("dbname=%s " % dbname      conn = psycopg.connect("dbname=%s " % dbname
# Line 486  def upload_shapefile(filename, db, table Line 521  def upload_shapefile(filename, db, table
521      #print stmt      #print stmt
522    
523      numshapes, shapetype, mins, maxs = shp.info()      numshapes, shapetype, mins, maxs = shp.info()
524      if shapetype == shapelib.SHPT_POINT:      wkttype =  shp_to_wkt[shapetype]
525          convert = point_to_wkt      if force_wkt_type:
526          wkttype = "POINT"          wkttype = force_wkt_type
527      elif shapetype == shapelib.SHPT_POLYGON:      convert = wkt_converter[wkttype]
         convert = polygon_to_wkt  
         wkttype = "POLYGON"  
     elif shapetype == shapelib.SHPT_ARC:  
         convert = arc_to_wkt  
         wkttype = "MULTILINESTRING"  
     else:  
         raise ValueError("Unsupported Shapetype %r" % shapetype)  
528    
529      cursor.execute("select AddGeometryColumn('%(dbname)s',"      cursor.execute("select AddGeometryColumn('%(dbname)s',"
530                     "'%(tablename)s', 'the_geom', '-1', '%(wkttype)s', 2);"                     "'%(tablename)s', 'the_geom', '-1', '%(wkttype)s', 2);"

Legend:
Removed from v.1634  
changed lines
  Added in v.1656

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26