[GRASS-SVN] r55454 - grass/trunk/lib/python/pygrass/vector

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Mar 19 08:24:57 PDT 2013


Author: zarch
Date: 2013-03-19 08:24:57 -0700 (Tue, 19 Mar 2013)
New Revision: 55454

Added:
   grass/trunk/lib/python/pygrass/vector/find.py
Modified:
   grass/trunk/lib/python/pygrass/vector/Makefile
   grass/trunk/lib/python/pygrass/vector/abstract.py
Log:
Add the Finder class to the vector map

Modified: grass/trunk/lib/python/pygrass/vector/Makefile
===================================================================
--- grass/trunk/lib/python/pygrass/vector/Makefile	2013-03-19 15:24:47 UTC (rev 55453)
+++ grass/trunk/lib/python/pygrass/vector/Makefile	2013-03-19 15:24:57 UTC (rev 55454)
@@ -9,7 +9,7 @@
 PGDIR = $(GDIR)/pygrass
 DSTDIR= $(PGDIR)/vector
 
-MODULES = abstract basic geometry sql table vector_type
+MODULES = abstract basic find geometry sql table vector_type
 
 PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
 PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)
@@ -29,4 +29,4 @@
 	$(INSTALL_DATA) $< $@
 
 #doxygen:
-DOXNAME = pythonpygrass
\ No newline at end of file
+DOXNAME = pythonpygrass

Modified: grass/trunk/lib/python/pygrass/vector/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/abstract.py	2013-03-19 15:24:47 UTC (rev 55453)
+++ grass/trunk/lib/python/pygrass/vector/abstract.py	2013-03-19 15:24:57 UTC (rev 55454)
@@ -12,6 +12,7 @@
 from grass.pygrass import functions
 from grass.pygrass.errors import GrassError, OpenError, must_be_open
 from table import DBlinks, Link
+from find import Finder
 
 
 def is_open(c_mapinfo):
@@ -361,6 +362,7 @@
             self.table = self.dblinks.by_layer(layer).table()
             self.n_lines = self.table.n_rows()
         self.writable = self.mapset == functions.getenv("MAPSET")
+        self.find = Finder(self.c_mapinfo, self.table, self.writable)
 
     def close(self):
         """Method to close the Vector"""

Added: grass/trunk/lib/python/pygrass/vector/find.py
===================================================================
--- grass/trunk/lib/python/pygrass/vector/find.py	                        (rev 0)
+++ grass/trunk/lib/python/pygrass/vector/find.py	2013-03-19 15:24:57 UTC (rev 55454)
@@ -0,0 +1,121 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Tue Mar 19 11:09:30 2013
+
+ at author: pietro
+"""
+import grass.lib.vector as libvect
+
+from grass.pygrass.errors import must_be_open
+
+from basic import Ilist
+from geometry import read_line, Isle, Area
+
+
+class Finder(object):
+    """Find the geomtry features of a vector map that are close to a point. ::
+
+        >>> from grass.pygrass.vector import VectorTopo
+        >>> zipcodes = VectorTopo('zipcodes', 'PERMANENT')
+        >>> schools = VectorTopo('schools', 'PERMANENT')
+        >>> zipcodes.open('r')
+        >>> schools.open('r')
+        >>> result = []
+        >>> for school in schools:
+        ...         zipcode = zipcodes.find.area(school)
+        ...         result.append((school.attrs['NAMESHORT'],
+        ...                        zipcode.attrs['ZIPCODE']))
+        ...
+        >>> result[0]
+        (u'SWIFT CREEK', u'RALEIGH 27606')
+        >>> result[1]
+        (u'BRIARCLIFF', u'CARY 27511')
+        >>> result[2]
+        (u'FARMINGTON WOODS', u'CARY 27511')
+        >>> from grass.pygrass.vector.geometry import Point
+        >>> pnt = Point(631213.349291, 224684.900084)
+        >>> school = schools.find.geo(pnt, maxdist=300.)
+        >>> school.attrs['NAMELONG']
+        u'ADAMS ELEMENTARY'
+        >>> for school in schools.find.geos(pnt, maxdist=1000.):
+        ...     print school.attrs['NAMELONG']
+        ...
+        CARY HIGH
+        EAST CARY MIDDLE SITE
+        ADAMS ELEMENTARY
+        >>> schools.close()
+        >>> zipcodes.close()
+    """
+    def __init__(self, c_mapinfo, table=None, writable=False):
+        """Find geometry feature around a point.
+        """
+        self.c_mapinfo = c_mapinfo
+        self.table = table
+        self.writable = writable
+        self.vtype = {'point':    libvect.GV_POINT,  # 1
+                      'line':     libvect.GV_LINE,   # 2
+                      'boundary': libvect.GV_BOUNDARY,  # 3
+                      'centroid': libvect.GV_CENTROID,  # 4
+                      'all': -1}  # -1
+
+# TODO: add the Node class and enable this method
+#    def node(self, point, maxdist):
+#        """Find the nearest node. Vect_find_node"""
+#        i = libvect.Vect_find_node(self.c_mapinfo, point.x, point.y, point.z,
+#                                   float(maxdist), int(not point.is2D))
+#        return geometry.Node(self.c_mapinfo.contents.plus.contents.Node[i])
+
+    @must_be_open
+    def geo(self, point, maxdist, type='all', exclude=0):
+        """Find the nearest line. Vect_find_line
+
+        Valid type are all the keys in find.vtype dictionary
+        """
+        feature_id = libvect.Vect_find_line(self.c_mapinfo,
+                                            point.x, point.y,
+                                            point.z if point.z else 0,
+                                            self.vtype[type], float(maxdist),
+                                            int(not point.is2D), exclude)
+        if feature_id:
+            return read_line(feature_id, self.c_mapinfo,
+                             self.table, self.writable)
+
+    @must_be_open
+    def geos(self, point, maxdist, type='all', exclude=[]):
+        """Find the nearest line. Vect_find_line_list
+
+        Valid type are all the keys in find.vtype dictionary
+        """
+        excl = Ilist(exclude)
+        found = Ilist()
+        if libvect.Vect_find_line_list(self.c_mapinfo,
+                                       point.x, point.y,
+                                       point.z if point.z else 0,
+                                       self.vtype[type], float(maxdist),
+                                       int(not point.is2D),
+                                       excl.c_ilist, found.c_ilist):
+            return [read_line(f_id, self.c_mapinfo, self.table, self.writable)
+                    for f_id in found]
+        else:
+            return []
+
+    @must_be_open
+    def area(self, point):
+        """Find the nearest area. Vect_find_area"""
+        area_id = libvect.Vect_find_area(self.c_mapinfo, point.x, point.y)
+        if area_id:
+            return Area(v_id=area_id, c_mapinfo=self.c_mapinfo,
+                        table=self.table, writable=self.writable)
+
+    @must_be_open
+    def island(self, point):
+        """Find the nearest island. Vect_find_island"""
+        isle_id = libvect.Vect_find_island(self.c_mapinfo, point.x, point.y)
+        if isle_id:
+            return Isle(v_id=isle_id, c_mapinfo=self.c_mapinfo,
+                        table=self.table, writable=self.writable)
+
+    def is_open(self):
+        """Check if the vector map is open or not"""
+        import abstract
+        return abstract.is_open(self.c_mapinfo)



More information about the grass-commit mailing list