You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

factory.py 961B

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