I am trying to convert a geo column into Polygons in Python with SQLite3. I imported the file into Python and geo rows look like following first row:

geo[0] = b'\x00\x01$i\x00\x00@\xd9\x94\x8b\xd6<\x1bAb\xda7\xb6]\xb1QA\xf0\xf7\x8b\x19UC\x1bA\x9c\xde\xc5\r\xc3\xb1QA|\x03\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00Hlw\xef-C\x1bA\x9c\xde\xc5\r\xc3\xb1QA\xf0\xf7\x8b\x19UC\x1bAv\xc0u)^\xb1QA\xbcw\xd4\x88\xf1<\x1bAb\xda7\xb6]\xb1QA\xa5\xdc}n\xd7<\x1bA\x84.\xe1r\xbe\xb1QA@\xd9\x94\x8b\xd6<\x1bA\xce\x8eT\xef\xc1\xb1QAHlw\xef-C\x1bA\x9c\xde\xc5\r\xc3\xb1QA\xfe'

When I use ST_AsText(GEO) in spatialite_gui, it makes the following conversion:

POLYGON((446667.483854 4638476.215202, 446677.274948 4638072.647812, 446268.383623 4638070.847159, 446261.857902 4638457.794994, 446261.63631 4638471.739536, 446667.483854 4638476.215202))

However, I could not achieve the same thing in Python using SQLite3 because it says there is no such function. Then, I started to look at alternative solutions and found this method: wkb.loads(parcel.the_geom, hex=True) with shapely package here. Yet, this also did not work and produced the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-120-96c04363008d> in <module>
      1 from shapely import wkb
----> 3 shp = wkb.loads(data[0][-1], hex=True)
      4 print(shp)

~/anaconda3/lib/python3.7/site-packages/shapely/wkb.py in loads(data, hex)
     13     reader = WKBReader(lgeos)
     14     if hex:
---> 15         return reader.read_hex(data)
     16     else:
     17         return reader.read(data)

~/anaconda3/lib/python3.7/site-packages/shapely/geos.py in read_hex(self, data)
    438         """Returns geometry from WKB hex"""
    439         if sys.version_info[0] >= 3:
--> 440             data = data.encode('ascii')
    441         geom = self._lgeos.GEOSWKBReader_readHEX(
    442             self._reader, c_char_p(data), c_size_t(len(data)))

AttributeError: 'bytes' object has no attribute 'encode'

How can I resolve this issue?

  • You can use SpatiaLite functions with Python as well. You need the mod_spatialite module and all the dependencies that mod_spatialite requires. There are Windows binaries available at gaia-gis.it/gaia-sins/index.html. Then you can open the SQLite connection and load mod_spatialite as an extension. – user30184 Oct 15 '20 at 9:14
  • I am a Mac user. Do you have any alternative solution? – tcokyasar Oct 15 '20 at 13:29
  • Try to find compiled binaries for Mac or instructions for compiling them yourself. This is very old answer but perhaps it gives some hints stackoverflow.com/questions/46303949/…. Try to find at least version 4.3. – user30184 Oct 15 '20 at 13:37
  • 1
    Found my answer here: gis.stackexchange.com/questions/184850/… Thanks for guidance! – tcokyasar Oct 15 '20 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.