Well, I have a python programm which parses files. The data in these files looks like this:
Type=0x21
Label=2428
Data1=(54.67346,59.00001),(54.67415,59.00242),(54.67758,59.00001)
This is my code, it sends a parsed data to MySql data base
import MySQLdb
f = open('test.mp', 'r')
db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="gis", charset='utf8')
cursor = db.cursor()
i=0
for line in f.readlines():
if (line.startswith("Type")):
type=line[5:]
if (line.startswith("Label")):
label=line[6:]
if (line.startswith("Data")):
data=line[6:]
sql="""INSERT INTO `polylines` (Type, Label, Data) VALUES ('%(Type)s', '%(Label)s', '%(Data)s')"""%{"Type":type, "Label":label, "Data":data}
cursor.execute(sql)
db.commit()
db.close()
f.close()
And I always get the same error -
_mysql_exceptions.OperationalError: (1416, 'Cannot get
geometry object from data you send to the GEOMETRY field')
I think it is because I send data in the Data variable to Linestring field in data base. I tried to change date that I am sending to look like (1 1,2 2,3 3), but I got this error again. How should I change the data and avoid this error?
Well, after some research and some tests i finally have found an answer. This problem was not with python, but with mysql.
First of all, we need our Data variable to look like Data="LineString(1 1,2 2,3 3)". Then, in Insert function we shoul write (GeomFromText('%(Data)s')) , to help mysql to get geometry from text. So, the whole Insert line looks like this:
sql="""INSERT INTO `polylines` (Type, Label, Data) VALUES ('%(Type)s', '%(Label)s', (GeomFromText('%(Data)s')))"""%{"Type":type, "Label":label, "Data":data}
Now it works!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With