Changeset 1546

Show
Ignore:
Timestamp:
02/09/10 04:12:10 (4 weeks ago)
Author:
seang
Message:

Gain some initial separation between ops and their GEOS implementations.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Shapely/branches/1.2/shapely/ops.py

    r1543 r1546  
    99from shapely.geometry import asShape, asLineString, asMultiLineString 
    1010 
    11 def shapeup(ob): 
    12     if isinstance(ob, BaseGeometry): 
    13         return ob 
    14     else: 
    15         try: 
    16             return asShape(ob) 
    17         except ValueError: 
    18             return asLineString(ob) 
    1911 
    20 def polygonize(iterator): 
    21     """Creates polygons from a list of LineString objects. 
    22     """ 
    23     lines = [shapeup(ob) for ob in iterator] 
    24     geom_array_type = c_void_p * len(lines) 
    25     geom_array = geom_array_type() 
    26     for i, line in enumerate(lines): 
    27         geom_array[i] = line._geom 
    28     product = lgeos.GEOSPolygonize(byref(geom_array), len(lines)) 
    29     collection = geom_factory(product) 
    30     for g in collection.geoms: 
    31         clone = lgeos.GEOSGeom_clone(g._geom) 
    32         g = geom_factory(clone) 
    33         g._owned = False 
    34         yield g 
     12class CollectionOperator(object): 
    3513 
    36 def linemerge(lines):  
    37     """Merges all connected lines. Returns a LineString or MultiLineString  
    38     when lines are not contiguous.  
     14    def shapeup(self, ob): 
     15        if isinstance(ob, BaseGeometry): 
     16            return ob 
     17        else: 
     18            try: 
     19                return asShape(ob) 
     20            except ValueError: 
     21                return asLineString(ob) 
     22 
     23    def polygonize(self, lines): 
     24        """Creates polygons from a source of lines 
    3925         
    40     Parameters  
    41     ----------  
    42     lines : MultiLineString, sequence of lines, or sequence of coordinates 
    43     """  
    44     multilinestring = None  
    45     if hasattr(lines, 'type') and lines.type == 'MultiLineString':  
    46         multilinestring = lines  
    47     elif hasattr(lines, '__iter__'):  
    48         try:  
    49             multilinestring = asMultiLineString([ls.coords for ls in lines])  
    50         except AttributeError:  
    51             multilinestring = asMultiLineString(lines)  
    52     if multilinestring is None:  
    53         raise ValueError("Cannot linemerge %s" % lines) 
    54     result = lgeos.GEOSLineMerge(multilinestring._geom)  
    55     return geom_factory(result)    
     26        The source may be a MultiLineString, a sequence of LineString objects, 
     27        or a sequence of objects than can be adapted to LineStrings. 
     28        """ 
     29        source = getattr(lines, 'geoms', None) or lines 
     30        obs = [self.shapeup(l) for l in source] 
     31        geom_array_type = c_void_p * len(obs) 
     32        geom_array = geom_array_type() 
     33        for i, line in enumerate(obs): 
     34            geom_array[i] = line._geom 
     35        product = lgeos.GEOSPolygonize(byref(geom_array), len(obs)) 
     36        collection = geom_factory(product) 
     37        for g in collection.geoms: 
     38            clone = lgeos.GEOSGeom_clone(g._geom) 
     39            g = geom_factory(clone) 
     40            g._owned = False 
     41            yield g 
    5642 
    57 def cascaded_union(geoms): 
    58     """Returns the union of a sequence of geometries 
    59      
    60     This is the most efficient method of dissolving many polygons. 
    61     """ 
    62     L = len(geoms) 
    63     subs = (c_void_p * L)() 
    64     for i, g in enumerate(geoms): 
    65         subs[i] = g._geom 
    66     collection = lgeos.GEOSGeom_createCollection(6, subs, L) 
    67     return geom_factory(lgeos.GEOSUnionCascaded(collection)) 
     43    def linemerge(self, lines):  
     44        """Merges all connected lines from a source 
     45         
     46        The source may be a MultiLineString, a sequence of LineString objects, 
     47        or a sequence of objects than can be adapted to LineStrings.  Returns a 
     48        LineString or MultiLineString when lines are not contiguous.  
     49        """  
     50        source = None  
     51        if hasattr(lines, 'type') and lines.type == 'MultiLineString':  
     52            source = lines  
     53        elif hasattr(lines, '__iter__'):  
     54            try:  
     55                source = asMultiLineString([ls.coords for ls in lines])  
     56            except AttributeError:  
     57                source = asMultiLineString(lines)  
     58        if source is None:  
     59            raise ValueError("Cannot linemerge %s" % lines) 
     60        result = lgeos.GEOSLineMerge(source._geom)  
     61        return geom_factory(result)    
    6862 
     63    def cascaded_union(geoms): 
     64        """Returns the union of a sequence of geometries 
     65         
     66        This is the most efficient method of dissolving many polygons. 
     67        """ 
     68        L = len(geoms) 
     69        subs = (c_void_p * L)() 
     70        for i, g in enumerate(geoms): 
     71            subs[i] = g._geom 
     72        collection = lgeos.GEOSGeom_createCollection(6, subs, L) 
     73        return geom_factory(lgeos.GEOSUnionCascaded(collection)) 
     74 
     75operator = CollectionOperator() 
     76polygonize = operator.polygonize 
     77linemerge = operator.linemerge 
     78cascaded_union = operator.cascaded_union 
     79