[GRASS-SVN] r68474 - in grass-addons/grass7/vector: . v.what.spoly
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri May 20 03:10:21 PDT 2016
Author: amuriy
Date: 2016-05-20 03:10:21 -0700 (Fri, 20 May 2016)
New Revision: 68474
Added:
grass-addons/grass7/vector/v.what.spoly/
grass-addons/grass7/vector/v.what.spoly/Makefile
grass-addons/grass7/vector/v.what.spoly/v.what.spoly.html
grass-addons/grass7/vector/v.what.spoly/v.what.spoly.py
Log:
<v.what.spoly> script added
Added: grass-addons/grass7/vector/v.what.spoly/Makefile
===================================================================
--- grass-addons/grass7/vector/v.what.spoly/Makefile (rev 0)
+++ grass-addons/grass7/vector/v.what.spoly/Makefile 2016-05-20 10:10:21 UTC (rev 68474)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.what.spoly
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Added: grass-addons/grass7/vector/v.what.spoly/v.what.spoly.html
===================================================================
--- grass-addons/grass7/vector/v.what.spoly/v.what.spoly.html (rev 0)
+++ grass-addons/grass7/vector/v.what.spoly/v.what.spoly.html 2016-05-20 10:10:21 UTC (rev 68474)
@@ -0,0 +1,23 @@
+<h2>DESCRIPTION</h2>
+
+<em>v.what.spoly</em> queries vector map with overlaping "spaghetti" polygons (e.g. Landsat footprints) at given location. Polygons must have not intersected boundaries (not cleaned).
+
+<h2>NOTES</h2>
+
+Module only runs in locations with Cartesian coordinates. Module needs GDAL utilities and GDAL-Python to be installed.
+
+<h2>EXAMPLES</h2>
+
+<div class="code"><pre>
+ v.what.spoly.py input=poly out=poly_select coor=465113.204082,5436513.2449
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="v.what.html">v.what</a>
+</em>
+
+<h2>AUTHOR</h2>
+
+Alexander Muriy (IEG RAS, Moscow)
Added: grass-addons/grass7/vector/v.what.spoly/v.what.spoly.py
===================================================================
--- grass-addons/grass7/vector/v.what.spoly/v.what.spoly.py (rev 0)
+++ grass-addons/grass7/vector/v.what.spoly/v.what.spoly.py 2016-05-20 10:10:21 UTC (rev 68474)
@@ -0,0 +1,167 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+############################################################################
+#
+# MODULE: v.what.spoly
+# AUTHOR(S): Alexander Muriy
+# (Institute of Environmental Geoscience, Moscow, Russia)
+# e-mail: amuriy AT gmail DOT com
+#
+# PURPOSE: Queries vector map with overlaping "spaghetti" polygons (e.g. Landsat footprints) at given location
+#
+# COPYRIGHT: (C) 2016 Alexander Muriy / GRASS Development Team
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+############################################################################
+#%Module
+#% description: Queries vector map with overlaping "spaghetti" polygons (e.g. Landsat footprints) at given location. Polygons must have not intersected boundaries.
+#% keywords: vector
+#%End
+#%Option
+#% key: input
+#% type: string
+#% required: yes
+#% multiple: no
+#% key_desc: name
+#% description: Name of input polygon vector map
+#% gisprompt: old,vector,vector
+#%End
+#%Option
+#% key: output
+#% type: string
+#% required: no
+#% multiple: no
+#% key_desc: name
+#% description: Name of output vector map
+#% gisprompt: new,vector,vector
+#%End
+#%Option
+#% key: coor
+#% type: string
+#% required: yes
+#% multiple: no
+#% key_desc: name
+#% description: Coordinates to query
+#%End
+#%Flag
+#% key: p
+#% description: Only print selected polygons
+#%End
+############################################################################
+
+import sys
+import os
+import glob
+import atexit
+
+
+try:
+ import grass.script as grass
+except:
+ try:
+ from grass.script import core as grass
+ except:
+ if not os.environ.has_key("GISBASE"):
+ print "You must be in GRASS GIS to run this program."
+ sys.exit(1)
+
+try:
+ from osgeo import ogr
+except:
+ print "Please install GDAL-Python bindings or add them to PYTHONPATH"
+ sys.exit(1)
+
+
+def cleanup():
+ inmap = options['input']
+ nuldev = file(os.devnull, 'w')
+ grass.try_remove(tmp)
+ for f in glob.glob(tmp + '*'):
+ grass.try_remove(f)
+ grass.run_command('g.remove', type_ = 'vect', pat = 'v_temp*', flags = 'f',
+ quiet = True, stderr = nuldev)
+
+def main():
+ inmap = options['input']
+ outmap = options['output']
+ coor = options['coor']
+ coor = coor.replace(',',' ')
+
+ global tmp, nuldev, grass_version
+ nuldev = None
+
+ # setup temporary files
+ tmp = grass.tempfile()
+
+ # check for LatLong location
+ if grass.locn_is_latlong() == True:
+ grass.fatal("Module works only in locations with cartesian coordinate system")
+
+ # check if input file exists
+ if not grass.find_file(inmap, element = 'vector')['file']:
+ grass.fatal(_("<%s> does not exist.") % inmap)
+
+
+ ## DO IT ##
+ ## add categories to boundaries
+ grass.run_command('v.category', input_ = inmap, option = 'add',\
+ type_ = 'boundary', output = 'v_temp_bcats', \
+ quiet = True, stderr = nuldev)
+
+ ## export polygons to CSV + WKT
+ tmp1 = tmp + '.csv'
+ tmp2 = tmp + '2.csv'
+ grass.run_command('v.out.ogr', input_ = 'v_temp_bcats', output = tmp1,
+ format_ = "CSV", type_ = ('boundary'),
+ lco = "GEOMETRY=AS_WKT", quiet = True, stderr = nuldev)
+
+ ## convert lines to polygons
+ f1 = open(tmp1, 'r')
+ f2 = open(tmp2, 'w')
+ for line in f1:
+ f2.write(line.replace('LINESTRING','POLYGON').replace(' (',' ((').replace(')"','))"'))
+ f1.close()
+ f2.close()
+
+ with open(tmp2, 'r') as f:
+ print f.read()
+
+ ## open CSV with OGR and get layer name
+ f = ogr.Open(tmp2, 0)
+ lyr = f.GetLayer(0)
+ lyr_name = lyr.GetName()
+
+ ## make spatial query with coordinates
+ coords = "%s %s" % (coor, coor)
+ tmp3 = tmp + '_v_temp_select.shp'
+ cmd = 'ogr2ogr ' + ' -spat ' + coords + ' ' + tmp3 + ' ' + tmp2 + ' ' + lyr_name
+ os.system(cmd)
+
+ ## open SHP with OGR and get layer name
+ f = ogr.Open(tmp3, 0)
+ lyr = f.GetLayer(0)
+ lyr_name = lyr.GetName()
+
+ ## print selected objects to stdout or write into vector map
+ if flags['p']:
+ cmd = 'ogrinfo -al -fields=YES -geom=SUMMARY' + ' ' + tmp3 + ' ' + lyr_name
+ os.system(cmd)
+ else:
+ grass.run_command('v.in.ogr', input_ = tmp3, layer = lyr_name, \
+ output = outmap, flags = 'c', quiet = True, stderr = nuldev)
+
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ atexit.register(cleanup)
+ main()
+
Property changes on: grass-addons/grass7/vector/v.what.spoly/v.what.spoly.py
___________________________________________________________________
Added: svn:executable
+ *
More information about the grass-commit
mailing list