From: <js...@us...> - 2007-11-12 16:51:15
|
Revision: 4225 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4225&view=rev Author: jswhit Date: 2007-11-12 08:51:08 -0800 (Mon, 12 Nov 2007) Log Message: ----------- add Shapely Added Paths: ----------- trunk/toolkits/basemap-testing/lib/shapely/geos.py trunk/toolkits/basemap-testing/lib/shapely/iterops.py trunk/toolkits/basemap-testing/lib/shapely/predicates.py trunk/toolkits/basemap-testing/lib/shapely/topology.py trunk/toolkits/basemap-testing/lib/shapely/wkb.py trunk/toolkits/basemap-testing/lib/shapely/wkt.py Added: trunk/toolkits/basemap-testing/lib/shapely/geos.py =================================================================== --- trunk/toolkits/basemap-testing/lib/shapely/geos.py (rev 0) +++ trunk/toolkits/basemap-testing/lib/shapely/geos.py 2007-11-12 16:51:08 UTC (rev 4225) @@ -0,0 +1,47 @@ +""" +Exports the libgeos_c shared lib, GEOS-specific exceptions, and utilities. +""" + +import atexit +from ctypes import CDLL, CFUNCTYPE, c_char_p +from ctypes.util import find_library +import sys + +from find_geoslib import find_geoslib +lgeos = find_geoslib() + +# Exceptions + +class ReadingError(Exception): + pass + +class DimensionError(Exception): + pass + +class TopologicalError(Exception): + pass + +class PredicateError(Exception): + pass + + +# GEOS error handlers, which currently do nothing. + +def error_handler(fmt, list): + pass +error_h = CFUNCTYPE(None, c_char_p, c_char_p)(error_handler) + +def notice_handler(fmt, list): + pass +notice_h = CFUNCTYPE(None, c_char_p, c_char_p)(notice_handler) + +# Register a cleanup function + +def cleanup(): + lgeos.finishGEOS() + +atexit.register(cleanup) + +lgeos.initGEOS(notice_h, error_h) + + Added: trunk/toolkits/basemap-testing/lib/shapely/iterops.py =================================================================== --- trunk/toolkits/basemap-testing/lib/shapely/iterops.py (rev 0) +++ trunk/toolkits/basemap-testing/lib/shapely/iterops.py 2007-11-12 16:51:08 UTC (rev 4225) @@ -0,0 +1,54 @@ +""" +""" + +from ctypes import c_char_p, c_size_t +from shapely.geos import lgeos + + +def geos_from_geometry(geom): + data = geom.to_wkb() + return lgeos.GEOSGeomFromWKB_buf( + c_char_p(data), + c_size_t(len(data)) + ) + +class BinaryPredicateIterator(object): + + """A generating non-data descriptor. + """ + + fn = None + context = None + + def __init__(self, fn): + self.fn = fn + + def __get__(self, obj, objtype=None): + self.context = obj + return self + + def __call__(self, geom, iterator, value=True): + geos_geom = geos_from_geometry(geom) + for item in iterator: + try: + geom, ob = item + except TypeError: + geom = item + ob = geom + retval = self.fn(geos_geom, geos_from_geometry(geom)) + if retval == 2: + raise PredicateError, "Failed to evaluate %s" % repr(self.fn) + elif bool(retval) == value: + yield ob + + +# utilities +disjoint = BinaryPredicateIterator(lgeos.GEOSDisjoint) +touches = BinaryPredicateIterator(lgeos.GEOSTouches) +intersects = BinaryPredicateIterator(lgeos.GEOSIntersects) +crosses = BinaryPredicateIterator(lgeos.GEOSCrosses) +within = BinaryPredicateIterator(lgeos.GEOSWithin) +contains = BinaryPredicateIterator(lgeos.GEOSContains) +overlaps = BinaryPredicateIterator(lgeos.GEOSOverlaps) +equals = BinaryPredicateIterator(lgeos.GEOSEquals) + Added: trunk/toolkits/basemap-testing/lib/shapely/predicates.py =================================================================== --- trunk/toolkits/basemap-testing/lib/shapely/predicates.py (rev 0) +++ trunk/toolkits/basemap-testing/lib/shapely/predicates.py 2007-11-12 16:51:08 UTC (rev 4225) @@ -0,0 +1,50 @@ +""" +Support for GEOS spatial predicates. +""" + +from shapely.geos import PredicateError + +# Predicates + +class BinaryPredicate(object): + + """A callable non-data descriptor. + """ + + fn = None + context = None + + def __init__(self, fn): + self.fn = fn + + def __get__(self, obj, objtype=None): + self.context = obj + return self + + def __call__(self, other): + retval = self.fn(self.context._geom, other._geom) + if retval == 2: + raise PredicateError, "Failed to evaluate %s" % repr(self.fn) + return bool(retval) + + +# A data descriptor +class UnaryPredicate(object): + + """A data descriptor. + """ + + fn = None + + def __init__(self, fn): + self.fn = fn + + def __get__(self, obj, objtype=None): + retval = self.fn(obj._geom) + if retval == 2: + raise PredicateError, "Failed to evaluate %s" % repr(self.fn) + return bool(retval) + + def __set__(self, obj, value=None): + raise AttributeError, "Attribute is read-only" + Added: trunk/toolkits/basemap-testing/lib/shapely/topology.py =================================================================== --- trunk/toolkits/basemap-testing/lib/shapely/topology.py (rev 0) +++ trunk/toolkits/basemap-testing/lib/shapely/topology.py 2007-11-12 16:51:08 UTC (rev 4225) @@ -0,0 +1,62 @@ +""" +Support for GEOS topological operations. +""" + +from shapely.geos import TopologicalError + + +class BinaryTopologicalOp(object): + + """A callable non-data descriptor. + + Wraps a GEOS function. The factory is a callable which wraps results in + the appropriate shapely geometry class. + """ + + fn = None + context = None + factory = None + + def __init__(self, fn, factory): + self.fn = fn + self.factory = factory + + def __get__(self, obj, objtype=None): + self.context = obj + return self + + def __call__(self, other): + product = self.fn(self.context._geom, other._geom) + if not product: + # Check validity of geometries + if not self.context.is_valid: + raise TopologicalError, \ + "The operation '%s' produced a null geometry. Likely cause is invalidity of the geometry %s" % (self.fn.__name__, repr(self.context)) + elif not other.is_valid: + raise TopologicalError, \ + "The operation '%s' produced a null geometry. Likely cause is invalidity of the 'other' geometry %s" % (self.fn.__name__, repr(other)) + + return self.factory(product) + + +class UnaryTopologicalOp(object): + + """A data descriptor. + + Wraps a GEOS function. The factory is a callable which wraps results in + the appropriate shapely geometry class. + """ + + fn = None + factory = None + + def __init__(self, fn, factory): + self.fn = fn + self.factory = factory + + def __get__(self, obj, objtype=None): + return self.factory(self.fn(obj._geom)) + + def __set__(self, obj, value=None): + raise AttributeError, "Attribute is read-only" + Added: trunk/toolkits/basemap-testing/lib/shapely/wkb.py =================================================================== --- trunk/toolkits/basemap-testing/lib/shapely/wkb.py (rev 0) +++ trunk/toolkits/basemap-testing/lib/shapely/wkb.py 2007-11-12 16:51:08 UTC (rev 4225) @@ -0,0 +1,35 @@ +""" +Load/dump geometries using the well-known binary (WKB) format. +""" + +from ctypes import byref, c_int, c_size_t, c_char_p, string_at + +from shapely.geos import lgeos, ReadingError +from shapely.geometry.base import geom_factory + + +# Pickle-like convenience functions + +def loads(data): + """Load a geometry from a WKB string.""" + geom = lgeos.GEOSGeomFromWKB_buf(c_char_p(data), c_size_t(len(data))); + if not geom: + raise ReadingError, \ + "Could not create geometry because of errors while reading input." + return geom_factory(geom) + +def load(fp): + """Load a geometry from an open file.""" + data = fp.read() + return loads(data) + +def dumps(ob): + """Dump a WKB representation of a geometry to a byte string.""" + size = c_int() + bytes = lgeos.GEOSGeomToWKB_buf(ob._geom, byref(size)) + return string_at(bytes, size.value) + +def dump(ob, fp): + """Dump a geometry to an open file.""" + fp.write(dumps(ob)) + Added: trunk/toolkits/basemap-testing/lib/shapely/wkt.py =================================================================== --- trunk/toolkits/basemap-testing/lib/shapely/wkt.py (rev 0) +++ trunk/toolkits/basemap-testing/lib/shapely/wkt.py 2007-11-12 16:51:08 UTC (rev 4225) @@ -0,0 +1,33 @@ +""" +Load/dump geometries using the well-known text (WKT) format. +""" + +from ctypes import byref, c_int, c_size_t, c_char_p, string_at + +from shapely.geos import lgeos, ReadingError +from shapely.geometry.base import geom_factory + + +# Pickle-like convenience functions + +def loads(data): + """Load a geometry from a WKT string.""" + geom = lgeos.GEOSGeomFromWKT(c_char_p(data)) + if not geom: + raise ReadingError, \ + "Could not create geometry because of errors while reading input." + return geom_factory(geom) + +def load(fp): + """Load a geometry from an open file.""" + data = fp.read() + return loads(data) + +def dumps(ob): + """Dump a WKB representation of a geometry to a byte string.""" + return string_at(lgeos.GEOSGeomToWKT(ob._geom)) + +def dump(ob, fp): + """Dump a geometry to an open file.""" + fp.write(dumps(ob)) + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |