MCPcopy
hub / github.com/django/django / fromfile

Function fromfile

django/contrib/gis/geos/factory.py:4–28  ·  view source on GitHub ↗

Given a string file name, returns a GEOSGeometry. The file may contain WKB, WKT, or HEX.

(file_h)

Source from the content-addressed store, hash-verified

2
3
4def 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
31def fromstr(string, **kwargs):

Callers 1

test_fromfileMethod · 0.90

Calls 4

GEOSGeometryClass · 0.90
readMethod · 0.45
decodeMethod · 0.45
matchMethod · 0.45

Tested by 1

test_fromfileMethod · 0.72