Debian Codegraph @

django/contrib/gis/geos/factory.py

raw
1 from django.contrib.gis.geos.geometry import GEOSGeometry, hex_regex, wkt_regex
2
3
4 def fromfile(file_h):
5 """
6 Given a string file name, returns a GEOSGeometry. The file may contain WKB,
7 WKT, or HEX.
8 """
9 # If given a file name, get a real handle.
10 if isinstance(file_h, str):
11 with open(file_h, "rb") as file_h:
12 buf = file_h.read()
13 else:
14 buf = file_h.read()
15
16 # If we get WKB need to wrap in memoryview(), so run through regexes.
17 if isinstance(buf, bytes):
18 try:
19 decoded = buf.decode()
20 except UnicodeDecodeError:
21 pass
22 else:
23 if wkt_regex.match(decoded) or hex_regex.match(decoded):
24 return GEOSGeometry(decoded)
25 else:
26 return GEOSGeometry(buf)
27
28 return GEOSGeometry(memoryview(buf))
29
30
31 def fromstr(string, **kwargs):
32 "Given a string value, return a GEOSGeometry object."
33 return GEOSGeometry(string, **kwargs)