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
2
----> 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?