2
The goal of this module is to be a ctypes wrapper around the GEOS library
3
that will work on both *NIX and Windows systems. Specifically, this uses
6
I have several motivations for doing this:
7
(1) The GEOS SWIG wrapper is no longer maintained, and requires the
9
(2) The PCL implementation is over 2K+ lines of C and would make
10
PCL a requisite package for the GeoDjango application stack.
11
(3) Windows and Mac compatibility becomes substantially easier, and does not
12
require the additional compilation of PCL or GEOS and SWIG -- all that
13
is needed is a Win32 or Mac compiled GEOS C library (dll or dylib)
14
in a location that Python can read (e.g. 'C:\Python25').
16
In summary, I wanted to wrap GEOS in a more maintainable and portable way using
17
only Python and the excellent ctypes library (now standard in Python 2.5).
19
In the spirit of loose coupling, this library does not require Django or
20
GeoDjango. Only the GEOS C library and ctypes are needed for the platform
23
For more information about GEOS:
24
http://geos.refractions.net
26
For more info about PCL and the discontinuation of the Python GEOS
27
library see Sean Gillies' writeup (and subsequent update) at:
28
http://zcologia.com/news/150/geometries-for-python/
29
http://zcologia.com/news/429/geometries-for-python-update/
2
The GeoDjango GEOS module. Please consult the GeoDjango documentation
4
http://geodjango.org/docs/geos.html
31
from django.contrib.gis.geos.base import GEOSGeometry, wkt_regex, hex_regex
32
from django.contrib.gis.geos.geometries import Point, LineString, LinearRing, Polygon, HAS_NUMPY
6
from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex
7
from django.contrib.gis.geos.point import Point
8
from django.contrib.gis.geos.linestring import LineString, LinearRing
9
from django.contrib.gis.geos.polygon import Polygon
33
10
from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon
34
11
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
35
from django.contrib.gis.geos.libgeos import geos_version, geos_version_info
37
def fromfile(file_name):
39
Given a string file name, returns a GEOSGeometry. The file may contain WKB,
42
fh = open(file_name, 'rb')
45
if wkt_regex.match(buf) or hex_regex.match(buf):
46
return GEOSGeometry(buf)
48
return GEOSGeometry(buffer(buf))
50
def fromstr(wkt_or_hex, **kwargs):
51
"Given a string value (wkt or hex), returns a GEOSGeometry object."
52
return GEOSGeometry(wkt_or_hex, **kwargs)
55
"Converts HEXEWKB into WKT."
56
return GEOSGeometry(hex).wkt
59
"Converts WKT into HEXEWKB."
60
return GEOSGeometry(wkt).hex
63
"Returns the centroid of the geometry (given in HEXEWKB)."
64
return GEOSGeometry(input).centroid.wkt
67
"Returns the area of the geometry (given in HEXEWKB)."
68
return GEOSGeometry(input).area
12
from django.contrib.gis.geos.io import WKTReader, WKTWriter, WKBReader, WKBWriter
13
from django.contrib.gis.geos.factory import fromfile, fromstr
14
from django.contrib.gis.geos.libgeos import geos_version, geos_version_info, GEOS_PREPARE