Changeset 1108
- Timestamp:
- 05/20/08 01:06:15
- Files:
-
- Shapely/trunk/CHANGES.txt (modified) (1 diff)
- Shapely/trunk/manual/manual.txt (modified) (3 diffs)
- Shapely/trunk/shapely/geometry/geo.py (modified) (1 diff)
- Shapely/trunk/shapely/geometry/linestring.py (modified) (4 diffs)
- Shapely/trunk/shapely/iterops.py (modified) (3 diffs)
- Shapely/trunk/shapely/ops.py (modified) (2 diffs)
- Shapely/trunk/tests/polygonize.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
Shapely/trunk/CHANGES.txt
r1106 r1108 1 1 All tickets are children of http://trac.gispython.org/projects/PCL/ticket/ 2 2 3 1.0.5 3 1.0.5 (2008-05-20) 4 4 ------------------ 5 5 - Added access to GEOS polygonizer function. 6 - Raise exception when insufficient coordinate tuples are passed to LinearRing 7 constructor (#164). 6 8 7 9 1.0.4 (2008-05-01) Shapely/trunk/manual/manual.txt
r1107 r1108 5 5 :Author: Sean Gillies 6 6 :address: sgillies@frii.com 7 http://zcologia.com/sgillies 8 9 :revision: 1.0 10 :date: 2 January 2008 11 12 :copyright: This work is licensed under a `Creative Commons Attribution 3.0 United States License`__. 7 :revision: 1.0.5 8 :date: 20 May 2008 9 :copyright: This work is licensed under a `Creative Commons Attribution 3.0 10 United States License`__. 13 11 14 12 .. __: http://creativecommons.org/licenses/by/3.0/us/ 15 13 16 :abstract: This document describes the Shapely Python package for programming with geospatial geometries. 14 :abstract: This document describes the Shapely Python package for programming 15 with geospatial geometries. 17 16 18 17 .. sectnum:: … … 346 345 ---------------- 347 346 348 Polygonize 349 ++++++++++ 350 351 polygonize 352 >>> from shapely.geometry import LineString 353 >>> from shapely.geometry import polygonize 354 >>> lines = [ 355 ... LineString(((0, 0), (1, 1))), 356 ... LineString(((0, 0), (0, 1))), 357 ... LineString(((0, 1), (1, 1))), 358 ... LineString(((1, 1), (1, 0))), 359 ... LineString(((1, 0), (0, 0))) 360 ... ] 361 >>> result = polygonize(lines) 362 >>> result 363 <shapely.geometry.collection.GeometryCollection object at ...> 364 >>> len(result.geoms) 365 2 366 >>> list(result.geoms) 367 [<shapely.geometry.polygon.Polygon object at ...>, <shapely.geometry.polygon.Polygon object at ...>] 347 Polygonization 348 ++++++++++++++ 349 350 shapely.ops.polygonize(lines) : iterator 351 Returns an iterator over polygons constructed from the *lines* iterator. The 352 elements of *lines* may be Shapely geometries, objects that provide the geo 353 interface, or Numpy arrays or Python sequences shaped like LineStrings. 354 355 .. code-block:: python 356 357 >>> from shapely.ops import polygonize 358 >>> lines = [ 359 ... ((0, 0), (1, 1)), 360 ... ((0, 0), (0, 1)), 361 ... ((0, 1), (1, 1)), 362 ... ((1, 1), (1, 0)), 363 ... ((1, 0), (0, 0)) 364 ... ] 365 >>> result = polygonize(lines) 366 >>> list(result.geoms) 367 [<shapely.geometry.polygon.Polygon object at ...>, <shapely.geometry.polygon.Polygon object at ...>] 368 368 369 369 … … 824 824 [(0.0, 0.0)] 825 825 826 If you want to copy coordinate data to a new geometry, use the 827 *shapely.geometry.shape* function instead. 828 826 829 .. _Python geo interface: http://trac.gispython.org/projects/PCL/wiki/PythonGeoInterface 827 830 Shapely/trunk/shapely/geometry/geo.py
r1087 r1108 44 44 ob = context 45 45 46 geom_type = ob.get("type").lower() 46 try: 47 geom_type = ob.get("type").lower() 48 except AttributeError: 49 raise ValueError, "Context does not provide geo interface" 47 50 48 51 if geom_type == "point": Shapely/trunk/shapely/geometry/linestring.py
r1089 r1108 17 17 assert len(array['shape']) == 2 18 18 m = array['shape'][0] 19 n = array['shape'][1] 20 assert m >= 2 19 if m < 2: 20 raise ValueError, "LineStrings must have at least 2 coordinate tuples" 21 try: 22 n = array['shape'][1] 23 except IndexError: 24 raise ValueError, "Input %s is the wrong shape for a LineString" % str(ob) 21 25 assert n == 2 or n == 3 22 26 … … 43 47 dz = None 44 48 if n == 3: 45 dz = c_double(cp[n*i+2]) 46 49 try: 50 dz = c_double(cp[n*i+2]) 51 except IndexError: 52 raise ValueError, "Inconsistent coordinate dimensionality" 53 47 54 # Because of a bug in the GEOS C API, 48 55 # always set X before Y … … 55 62 # Fall back on list 56 63 m = len(ob) 57 n = len(ob[0]) 58 assert m >= 2 64 if m < 2: 65 raise ValueError, "LineStrings must have at least 2 coordinate tuples" 66 try: 67 n = len(ob[0]) 68 except TypeError: 69 raise ValueError, "Input %s is the wrong shape for a LineString" % str(ob) 59 70 assert n == 2 or n == 3 60 71 … … 76 87 dz = None 77 88 if n == 3: 78 dz = c_double(coords[2]) 89 try: 90 dz = c_double(coords[2]) 91 except IndexError: 92 raise ValueError, "Inconsistent coordinate dimensionality" 79 93 80 94 # Because of a bug in the GEOS C API, Shapely/trunk/shapely/iterops.py
r1005 r1108 1 1 """ 2 Iterative forms of operations. 2 3 """ 3 4 … … 37 38 this_geom = item 38 39 ob = this_geom 39 if this_geom._geom is None:40 if not this_geom._geom: 40 41 raise ValueError, "Null geometry supports no operations" 41 42 retval = self.fn(geom._geom, this_geom._geom) … … 54 55 overlaps = BinaryPredicateIterator(lgeos.GEOSOverlaps) 55 56 equals = BinaryPredicateIterator(lgeos.GEOSEquals) 56 Shapely/trunk/shapely/ops.py
r1107 r1108 1 1 from shapely.geos import lgeos 2 from shapely.geometry.base import geom_factory 3 from ctypes import byref, c_void_p, c_double 2 from shapely.geometry.base import geom_factory, BaseGeometry 3 from shapely.geometry import asShape, asLineString 4 from ctypes import byref, c_void_p 4 5 5 def polygonize(lines): 6 def shapeup(ob): 7 if isinstance(ob, BaseGeometry): 8 return ob 9 else: 10 try: 11 return asShape(ob) 12 except ValueError: 13 return asLineString(ob) 14 15 def polygonize(iterator): 6 16 """Creates polygons from a list of LineString objects. 7 17 """ 18 lines = [shapeup(ob) for ob in iterator] 8 19 geom_array_type = c_void_p * len(lines) 9 20 geom_array = geom_array_type() … … 11 22 geom_array[i] = line._geom 12 23 product = lgeos.GEOSPolygonize(byref(geom_array), len(lines)) 13 return geom_factory(product) 24 collection = geom_factory(product) 25 for g in collection.geoms: 26 clone = lgeos.GEOSGeom_clone(g._geom) 27 g = geom_factory(clone) 28 g._owned = False 29 yield g Shapely/trunk/tests/polygonize.txt
r1107 r1108 2 2 ============ 3 3 4 >>> from shapely.geometry import LineString 4 >>> from shapely.geometry import LineString, Point 5 5 >>> from shapely.ops import polygonize 6 6 >>> lines = [ … … 10 10 ... LineString(((1, 1), (1, 0))), 11 11 ... LineString(((1, 0), (0, 0))), 12 ... LineString(((5, 5), (6, 6))) 12 ... LineString(((5, 5), (6, 6))), 13 ... Point(0, 0), 13 14 ... ] 14 15 >>> result = polygonize(lines) 15 >>> result 16 <shapely.geometry.collection.GeometryCollection object at ...> 17 >>> len(result.geoms) 18 2 19 >>> list(result.geoms) 16 >>> list(result) 20 17 [<shapely.geometry.polygon.Polygon object at ...>, <shapely.geometry.polygon.Polygon object at ...>] 18 19 >>> lines2 = [ 20 ... ((0, 0), (1, 1)), 21 ... ((0, 0), (0, 1)), 22 ... ((0, 1), (1, 1)), 23 ... ((1, 1), (1, 0)), 24 ... ((1, 0), (0, 0)), 25 ... ((5, 5), (6, 6)), 26 ... ] 27 >>> result2 = polygonize(lines2) 28 >>> list(result2) 29 [<shapely.geometry.polygon.Polygon object at ...>, <shapely.geometry.polygon.Polygon object at ...>]
