web/lib/django/contrib/gis/geos/factory.py
changeset 38 77b6da96e6f1
parent 0 0d40e90630ef
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/django/contrib/gis/geos/factory.py	Wed Jun 02 18:57:35 2010 +0200
@@ -0,0 +1,23 @@
+from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex
+
+def fromfile(file_h):
+    """
+    Given a string file name, returns a GEOSGeometry. The file may contain WKB,
+    WKT, or HEX.
+    """
+    # If given a file name, get a real handle.
+    if isinstance(file_h, basestring): 
+        file_h = open(file_h, 'rb')
+
+    # Reading in the file's contents,
+    buf = file_h.read()
+
+    # If we get WKB need to wrap in buffer(), so run through regexes.
+    if wkt_regex.match(buf) or hex_regex.match(buf):
+        return GEOSGeometry(buf)
+    else:
+        return GEOSGeometry(buffer(buf))
+
+def fromstr(string, **kwargs):
+    "Given a string value, returns a GEOSGeometry object."
+    return GEOSGeometry(string, **kwargs)