Changeset 1108

Show
Ignore:
Timestamp:
05/20/08 01:06:15
Author:
seang
Message:

Finished polygonizer

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Shapely/trunk/CHANGES.txt

    r1106 r1108  
    11All tickets are children of http://trac.gispython.org/projects/PCL/ticket/ 
    22 
    3 1.0.5 
     31.0.5 (2008-05-20) 
    44------------------ 
    55- Added access to GEOS polygonizer function. 
     6- Raise exception when insufficient coordinate tuples are passed to LinearRing 
     7  constructor (#164). 
    68 
    791.0.4 (2008-05-01) 
  • Shapely/trunk/manual/manual.txt

    r1107 r1108  
    55:Author: Sean Gillies 
    66: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`__. 
    1311 
    1412.. __: http://creativecommons.org/licenses/by/3.0/us/ 
    1513 
    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. 
    1716 
    1817.. sectnum:: 
     
    346345---------------- 
    347346 
    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 ...>] 
     347Polygonization 
     348++++++++++++++ 
     349 
     350shapely.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 ...>] 
    368368 
    369369 
     
    824824  [(0.0, 0.0)] 
    825825 
     826If you want to copy coordinate data to a new geometry, use the 
     827*shapely.geometry.shape* function instead. 
     828 
    826829.. _Python geo interface: http://trac.gispython.org/projects/PCL/wiki/PythonGeoInterface 
    827830 
  • Shapely/trunk/shapely/geometry/geo.py

    r1087 r1108  
    4444        ob = context 
    4545 
    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" 
    4750 
    4851    if geom_type == "point": 
  • Shapely/trunk/shapely/geometry/linestring.py

    r1089 r1108  
    1717        assert len(array['shape']) == 2 
    1818        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) 
    2125        assert n == 2 or n == 3 
    2226 
     
    4347            dz = None 
    4448            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 
    4754            # Because of a bug in the GEOS C API,  
    4855            # always set X before Y 
     
    5562        # Fall back on list 
    5663        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) 
    5970        assert n == 2 or n == 3 
    6071 
     
    7687            dz = None 
    7788            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" 
    7993         
    8094            # Because of a bug in the GEOS C API,  
  • Shapely/trunk/shapely/iterops.py

    r1005 r1108  
    11""" 
     2Iterative forms of operations. 
    23""" 
    34 
     
    3738                this_geom = item 
    3839                ob = this_geom 
    39             if this_geom._geom is None
     40            if not this_geom._geom
    4041                raise ValueError, "Null geometry supports no operations" 
    4142            retval = self.fn(geom._geom, this_geom._geom) 
     
    5455overlaps = BinaryPredicateIterator(lgeos.GEOSOverlaps) 
    5556equals = BinaryPredicateIterator(lgeos.GEOSEquals) 
    56  
  • Shapely/trunk/shapely/ops.py

    r1107 r1108  
    11from shapely.geos import lgeos 
    2 from shapely.geometry.base import geom_factory 
    3 from ctypes import byref, c_void_p, c_double 
     2from shapely.geometry.base import geom_factory, BaseGeometry 
     3from shapely.geometry import asShape, asLineString 
     4from ctypes import byref, c_void_p 
    45 
    5 def polygonize(lines): 
     6def 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 
     15def polygonize(iterator): 
    616    """Creates polygons from a list of LineString objects. 
    717    """ 
     18    lines = [shapeup(ob) for ob in iterator] 
    819    geom_array_type = c_void_p * len(lines) 
    920    geom_array = geom_array_type() 
     
    1122        geom_array[i] = line._geom 
    1223    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  
    22============ 
    33 
    4     >>> from shapely.geometry import LineString 
     4    >>> from shapely.geometry import LineString, Point 
    55    >>> from shapely.ops import polygonize 
    66    >>> lines = [ 
     
    1010    ...     LineString(((1, 1), (1, 0))), 
    1111    ...     LineString(((1, 0), (0, 0))), 
    12     ...     LineString(((5, 5), (6, 6))) 
     12    ...     LineString(((5, 5), (6, 6))), 
     13    ...     Point(0, 0), 
    1314    ...     ] 
    1415    >>> 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) 
    2017    [<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 ...>]