django/contrib/gis/geos/__init__.py
author Justin Bronn <jbronn@geodjango.org>
Sat Jan 31 15:18:50 2009 -0600 (3 years ago)
branchtrunk
changeset 138 466bece04a15
parent 1 911c7e1d6ffe
child 141 3282e3926790
permissions -rw-r--r--
Merged in patch from Aryeh Leib Taurog for #9877, adapting as necessary.
     1 from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex
     2 from django.contrib.gis.geos.point import Point
     3 from django.contrib.gis.geos.linestring import LineString, LinearRing
     4 from django.contrib.gis.geos.polygon import Polygon
     5 from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon
     6 from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
     7 from django.contrib.gis.geos.libgeos import geos_version, geos_version_info, GEOS_PREPARE
     8 
     9 def fromfile(file_name):
    10     """
    11     Given a string file name, returns a GEOSGeometry. The file may contain WKB,
    12     WKT, or HEX.
    13     """
    14     fh = open(file_name, 'rb')
    15     buf = fh.read()
    16     fh.close()
    17     if wkt_regex.match(buf) or hex_regex.match(buf):
    18         return GEOSGeometry(buf)
    19     else:
    20         return GEOSGeometry(buffer(buf))
    21 
    22 def fromstr(wkt_or_hex, **kwargs):
    23     "Given a string value (wkt or hex), returns a GEOSGeometry object."
    24     return GEOSGeometry(wkt_or_hex, **kwargs)
    25 
    26 def hex_to_wkt(hex):
    27     "Converts HEXEWKB into WKT."
    28     return GEOSGeometry(hex).wkt
    29 
    30 def wkt_to_hex(wkt):
    31     "Converts WKT into HEXEWKB."
    32     return GEOSGeometry(wkt).hex
    33 
    34 def centroid(input):
    35     "Returns the centroid of the geometry (given in HEXEWKB)."
    36     return GEOSGeometry(input).centroid.wkt
    37 
    38 def area(input):
    39     "Returns the area of the geometry (given in HEXEWKB)."
    40     return GEOSGeometry(input).area
    41