|
0
|
1 |
from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex |
|
|
2 |
|
|
|
3 |
def fromfile(file_h): |
|
|
4 |
""" |
|
|
5 |
Given a string file name, returns a GEOSGeometry. The file may contain WKB, |
|
|
6 |
WKT, or HEX. |
|
|
7 |
""" |
|
|
8 |
# If given a file name, get a real handle. |
|
|
9 |
if isinstance(file_h, basestring): |
|
|
10 |
file_h = open(file_h, 'rb') |
|
|
11 |
|
|
|
12 |
# Reading in the file's contents, |
|
|
13 |
buf = file_h.read() |
|
|
14 |
|
|
|
15 |
# If we get WKB need to wrap in buffer(), so run through regexes. |
|
|
16 |
if wkt_regex.match(buf) or hex_regex.match(buf): |
|
|
17 |
return GEOSGeometry(buf) |
|
|
18 |
else: |
|
|
19 |
return GEOSGeometry(buffer(buf)) |
|
|
20 |
|
|
|
21 |
def fromstr(string, **kwargs): |
|
|
22 |
"Given a string value, returns a GEOSGeometry object." |
|
|
23 |
return GEOSGeometry(string, **kwargs) |