[GRASS-SVN] r51963 - in grass-addons/grass7/vector: . v.build.pg
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Jun 3 12:10:31 PDT 2012
Author: martinl
Date: 2012-06-03 12:10:30 -0700 (Sun, 03 Jun 2012)
New Revision: 51963
Added:
grass-addons/grass7/vector/v.build.pg/
grass-addons/grass7/vector/v.build.pg/Makefile
grass-addons/grass7/vector/v.build.pg/v.build.pg.html
grass-addons/grass7/vector/v.build.pg/v.build.pg.py
Log:
addons: new module v.build.pg - to build PostGIS topology
Added: grass-addons/grass7/vector/v.build.pg/Makefile
===================================================================
--- grass-addons/grass7/vector/v.build.pg/Makefile (rev 0)
+++ grass-addons/grass7/vector/v.build.pg/Makefile 2012-06-03 19:10:30 UTC (rev 51963)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.build.pg
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Property changes on: grass-addons/grass7/vector/v.build.pg/Makefile
___________________________________________________________________
Added: svn:mime-type
+ text/x-makefile
Added: svn:eol-style
+ native
Added: grass-addons/grass7/vector/v.build.pg/v.build.pg.html
===================================================================
--- grass-addons/grass7/vector/v.build.pg/v.build.pg.html (rev 0)
+++ grass-addons/grass7/vector/v.build.pg/v.build.pg.html 2012-06-03 19:10:30 UTC (rev 51963)
@@ -0,0 +1,25 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.build.pg</em> builds PostGIS topology for feature tables linked
+via <em><a href="v.external.html">v.external</a></em>.
+
+<h2>NOTES</h2>
+
+Note
+that <a href="http://trac.osgeo.org/postgis/wiki/UsersWikiPostgisTopology">PostGIS
+Topology</a> extension is currently under
+development. <em>v.build.pg</em> requires PostGIS 2.0.0+.
+
+<p>
+Existing PostGIS topology can be overwrite by <b>--overwrite</b> flag.
+
+<h2>EXAMPLES</h2>
+
+ at TODO
+
+<h2>AUTHOR</h2>
+
+Martin Landa, Czech Technical University in Prague
+
+<p>
+<i>Last changed: $Date$</i>
Property changes on: grass-addons/grass7/vector/v.build.pg/v.build.pg.html
___________________________________________________________________
Added: svn:mime-type
+ text/html
Added: svn:keywords
+ Author Date Id
Added: svn:eol-style
+ native
Added: grass-addons/grass7/vector/v.build.pg/v.build.pg.py
===================================================================
--- grass-addons/grass7/vector/v.build.pg/v.build.pg.py (rev 0)
+++ grass-addons/grass7/vector/v.build.pg/v.build.pg.py 2012-06-03 19:10:30 UTC (rev 51963)
@@ -0,0 +1,135 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE: v.build.pg
+# AUTHOR(S): Martin Landa
+# PURPOSE: Builds PostGIS topology for PG-linked vector map.
+# COPYRIGHT: (C) 2012 by Martin Landa, and the GRASS Development Team
+#
+# This program is free software under the GNU General
+# Public License (>=v2). Read the file COPYING that
+# comes with GRASS for details.
+#
+#############################################################################
+
+#%module
+#% description: Builds PostGIS topology for PG-linked vector map.
+#% keywords: vector
+#% keywords: external
+#% keywords: PostGIS
+#% keywords: topology
+#% overwrite: yes
+#%end
+#%option G_OPT_V_MAP
+#% description:
+#%end
+#%option
+#% key: topo_schema
+#% description: Name of schema where to build PostGIS topology
+#% key_desc: name
+#% required: yes
+#%end
+#%option
+#% key: topo_column
+#% description: Name of topology column
+#% key_desc: name
+#% required: yes
+#% answer: topo
+#%end
+#%option
+#% key: tolerance
+#% description: Tolerance to snap input geometry to existing primitives
+#% type: double
+#% answer: 1
+#end
+
+import os
+import sys
+
+import grass.script as grass
+
+def execute(sql, msg = None, useSelect = True):
+ if useSelect:
+ cmd = 'select'
+ else:
+ cmd = 'execute'
+
+ ret = grass.run_command('db.%s' % cmd, sql = sql,
+ **pg_conn)
+ if ret == 0:
+ return
+
+ if msg:
+ grass.fatal(msg)
+ else:
+ grass.fatal(_("Unable to build PostGIS topology"))
+
+def main():
+ vmap = options['map']
+ curr_mapset = grass.gisenv()['MAPSET']
+ mapset = grass.find_file(name = vmap,
+ element = 'vector')['mapset']
+
+ # check if map exists in the current mapset
+ if not mapset:
+ grass.fatal(_("Vector map <%s> not found") % vmap)
+ if mapset != curr_mapset:
+ grass.fatal(_("Vector map <%s> not found in the current mapset") % vmap)
+
+ # check for format
+ vInfo = grass.vector_info(vmap)
+ if vInfo['format'] != 'PostGIS,PostgreSQL':
+ grass.fatal(_("Vector map <%s> is not a PG-link") % vmap)
+
+ # default connection
+ global pg_conn
+ pg_conn = { 'driver': 'pg',
+ 'database' : vInfo['pg_dbname'] }
+
+ # check if topology schema already exists
+ topo_found = False
+ ret = grass.db_select(sql = "SELECT COUNT(*) FROM topology.topology " \
+ "WHERE name = '%s'" % options['topo_schema'],
+ **pg_conn)
+
+ if not ret or int(ret[0][0]) == 1:
+ topo_found = True
+
+ if topo_found:
+ if int(os.getenv('GRASS_OVERWRITE', '0')) == 1:
+ # -> overwrite
+ grass.warning(_("Topology schema <%s> already exists and will be overwritten") % \
+ options['topo_schema'])
+ else:
+ grass.fatal(_("option <%s>: <%s> exists.") % \
+ ('topo_schema', options['topo_schema']))
+
+ # drop topo schema if exists
+ execute(sql = "SELECT topology.DropTopology('%s')" % options['topo_schema'],
+ msg = _("Unable to remove topology schema"))
+
+ # create topo schema
+ schema, table = vInfo['pg_table'].split('.')
+ grass.message(_("Create new topology schema..."))
+ execute("SELECT topology.createtopology('%s', find_srid('%s', '%s', '%s'), %s)" % \
+ (options['topo_schema'], schema, table, vInfo['geometry_column'], options['tolerance']))
+
+ # add topo column to the feature table
+ grass.message(_("Create new topology column..."))
+ execute("SELECT topology.AddTopoGeometryColumn('%s', '%s', '%s', '%s', '%s')" % \
+ (options['topo_schema'], schema, table, options['topo_column'], vInfo['feature_type']))
+
+ # build topology
+ grass.message(_("Building PostGIS topology..."))
+ execute("UPDATE %s SET %s = topology.toTopoGeom(%s, '%s', 1, %s)" % \
+ (table, options['topo_column'], vInfo['geometry_column'], options['topo_schema'], options['tolerance']),
+ useSelect = False)
+
+ # report summary
+ execute("SELECT topology.TopologySummary('%s')" % options['topo_schema'])
+
+ return 0
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ sys.exit(main())
Property changes on: grass-addons/grass7/vector/v.build.pg/v.build.pg.py
___________________________________________________________________
Added: svn:executable
+ *
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
More information about the grass-commit
mailing list