| 1 |
""" |
|---|
| 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 |
|---|
| 4 |
the GEOS C api. |
|---|
| 5 |
|
|---|
| 6 |
I have several motivations for doing this: |
|---|
| 7 |
(1) The GEOS SWIG wrapper is no longer maintained, and requires the |
|---|
| 8 |
installation of SWIG. |
|---|
| 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'). |
|---|
| 15 |
|
|---|
| 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). |
|---|
| 18 |
|
|---|
| 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 |
|---|
| 21 |
of your choice. |
|---|
| 22 |
|
|---|
| 23 |
For more information about GEOS: |
|---|
| 24 |
http://geos.refractions.net |
|---|
| 25 |
|
|---|
| 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/ |
|---|
| 30 |
""" |
|---|
| 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 |
|---|
| 33 |
from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon |
|---|
| 34 |
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError |
|---|
| 35 |
from django.contrib.gis.geos.libgeos import geos_version, geos_version_info |
|---|
| 36 |
|
|---|
| 37 |
def fromfile(file_name): |
|---|
| 38 |
""" |
|---|
| 39 |
Given a string file name, returns a GEOSGeometry. The file may contain WKB, |
|---|
| 40 |
WKT, or HEX. |
|---|
| 41 |
""" |
|---|
| 42 |
fh = open(file_name, 'rb') |
|---|
| 43 |
buf = fh.read() |
|---|
| 44 |
fh.close() |
|---|
| 45 |
if wkt_regex.match(buf) or hex_regex.match(buf): |
|---|
| 46 |
return GEOSGeometry(buf) |
|---|
| 47 |
else: |
|---|
| 48 |
return GEOSGeometry(buffer(buf)) |
|---|
| 49 |
|
|---|
| 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) |
|---|
| 53 |
|
|---|
| 54 |
def hex_to_wkt(hex): |
|---|
| 55 |
"Converts HEXEWKB into WKT." |
|---|
| 56 |
return GEOSGeometry(hex).wkt |
|---|
| 57 |
|
|---|
| 58 |
def wkt_to_hex(wkt): |
|---|
| 59 |
"Converts WKT into HEXEWKB." |
|---|
| 60 |
return GEOSGeometry(wkt).hex |
|---|
| 61 |
|
|---|
| 62 |
def centroid(input): |
|---|
| 63 |
"Returns the centroid of the geometry (given in HEXEWKB)." |
|---|
| 64 |
return GEOSGeometry(input).centroid.wkt |
|---|
| 65 |
|
|---|
| 66 |
def area(input): |
|---|
| 67 |
"Returns the area of the geometry (given in HEXEWKB)." |
|---|
| 68 |
return GEOSGeometry(input).area |
|---|
| 69 |
|
|---|