[GIS] Postgis create polygon extent/convex hull from spatial table

convex hullextentspostgispostgresqlsql

Please help,

I have a polygon layer representing building footprints. I want to create new view in postgis that will contain rectangle representing bounding box and also another view that will have convex hulls polygon of all records in buildings table.
I'm creating something like this:

CREATE OR REPLACE VIEW xxx.extent AS SELECT ST_ConvexHull(buildings_2013.geom) as geom from xxx.buildings_2013;

but I won't create spatial view. Could you please help?

Best Answer

What do you mean by no spatial view. You mean you are not seeing it in geometry_columns table or your viewer is not loading it? If you are using PostGIS 1.5 or lower, you have to manually add the view to the table geometry_columns table. In PostGIS 2.0+, it will show in list but won't show type. To get type you need to do cast it.

Also many views like QGIS for example are finicky and won't load something if it has no id it recognizes as a key. You can try throwing in the primary key like gid or something in the view. So PostGIS 2+ definition should look Something like:

CREATE OR REPLACE VIEW xxx.extent AS 
  SELECT gid, ST_ConvexHull(buildings_2013.geom)::geometry(POLYGON,4326) As geom 
   FROM xxx.buildings_2013;

replace 4326 with the SRID of your geom column. It has to be a constant for casting unfortunately so you can't just do geometry(POLYGON, ST_SRID(geom).