~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/gis/geos/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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/
 
2
The GeoDjango GEOS module.  Please consult the GeoDjango documentation
 
3
for more details: 
 
4
  http://geodjango.org/docs/geos.html
30
5
"""
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
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
 
    
 
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