[Featureserver] PostGIS - attributes_only

Josh Livni mailing_lists at umbrellaconsulting.com
Tue Jan 8 00:23:51 EST 2008


I've made a few modifications to my postgis datastore recently, to help 
handle some more 'gis' functionality (buffering, simplifying, text 
search, etc).  Since these are not applicable to all datastores, I don't 
think they're that helpful as modifications to the FeatureServer trunk, 
but if anyone is interested, please let me know and I'll post patches 
(they're really just a couple lines each).

Back to topic: here's one small patch that might be applicable to the 
trunk - it lets you specify (in the config file) which attribute columns 
you want to have returned from postgis.  I often have multiple geometry 
columns, and generally don't want the wkb of them returned -- I also 
sometimes only really want a small subset of my total postgis columns 
returned.  Of course you can just make a postgres view that has only 
columns of interest, but in case you're not into that, please feel free 
to use the attached patch.

Cheers,

  -Josh
-------------- next part --------------
Index: FeatureServer/DataSource/PostGIS.py
===================================================================
--- FeatureServer/DataSource/PostGIS.py	(revision 414)
+++ FeatureServer/DataSource/PostGIS.py	(working copy)
@@ -24,16 +24,18 @@
        FeatureServer."""
     wkt_linestring_match = re.compile(r'\(([^()]+)\)')
 
-    def __init__(self, name, srid = 4326, fid = "ogc_fid", geometry = "the_geom", order = "", writable = True, **args):
+    def __init__(self, name, srid = 4326, fid = "ogc_fid", geometry = "the_geom", order = "", attribute_cols = '*', writable = True, **args):
         DataSource.__init__(self, name, **args)
-        self.table      = args["layer"]
-        self.fid_col    = fid
-        self.geom_col   = geometry
-        self.order      = order
-        self.srid       = srid
-        self.db         = None
-        self.dsn        = args["dsn"]
-        self.writable   = writable
+        self.table          = args["layer"]
+        self.fid_col        = fid
+        self.geom_col       = geometry
+        self.order          = order
+        self.srid           = srid
+        self.db             = None
+        self.dsn            = args["dsn"]
+        self.writable       = writable
+        self.attribute_cols = attribute_cols
+
     def begin (self):
         self.db = psycopg.connect(self.dsn)
 
@@ -182,8 +184,7 @@
             if action.bbox:
                 filters.append( "%s && SetSRID('BOX3D(%f %f,%f %f)'::box3d, %s) and intersects(%s, SetSRID('BOX3D(%f %f,%f %f)'::box3d, %s))" % (
                                         (self.geom_col,) + tuple(action.bbox) + (self.srid,) + (self.geom_col,) + (tuple(action.bbox) + (self.srid,))))
-
-            sql = "SELECT AsText(%s) as fs_text_geom, * FROM \"%s\"" % (self.geom_col, self.table)
+            sql = "SELECT AsText(%s) as fs_text_geom, %s, %s FROM \"%s\"" % (self.geom_col, self.fid_col, self.attribute_cols, self.table)
             if filters:
                 sql += " WHERE " + " AND ".join(filters)
             if self.order:
@@ -194,7 +195,6 @@
                 sql += " LIMIT 1000"
             if action.startfeature:
                 sql += " OFFSET %d" % action.startfeature
-            
             try:
                 cursor.execute(str(sql)% attrs)
             except:
@@ -208,7 +208,8 @@
             geom  = self.from_wkt(props['fs_text_geom'])
             id = props[self.fid_col]
             del props[self.fid_col]
-            del props[self.geom_col]
+            if self.attribute_cols == '*':
+                del props[self.geom_col]
             del props['fs_text_geom']
             for key, value in props.items():
                 if isinstance(value, str): 
Index: doc/DataSources.txt
===================================================================
--- doc/DataSources.txt	(revision 414)
+++ doc/DataSources.txt	(working copy)
@@ -86,6 +86,7 @@
     layer=mylayer
     fid=ogc_fid
     geometry=wkb_geometry
+    attribute_cols=name,some_interesting_column #optional
 
 Dependancies:
  * OGR


More information about the Featureserver mailing list