Parsing Geometry MySQL's Field

1,779 views
Skip to first unread message

Silver

unread,
Feb 28, 2011, 2:18:56 AM2/28/11
to Google Maps JavaScript API v3
Hello friends,
I have a MySQL table for storing polygons for each country's state.
---
idregion, int, autoincrement
region, varchar(50)
polygon: geometry
---

From PHP I'm getting a json response with the polygon coordinates as
string:
"SELECT region, AsText(polygon) FROM regions"
Like this:
[{'region':'state 1','polygon':'POLYGON(5 3,8 4,5 5,2 9)'},
{'region':'state 2','polygon':'POLYGON(2 3,7 1,9 7,1 3)'},...]

Now, for showing the polygon using the Google Maps API I need to
remove the first portion of the string and parse the rest twice for
creating a LatLng set.

Can somebody suggest to me a best way for creating a polygon from a
GEOMETRY MySQL field?
Regards!!!!

Chris Broadfoot

unread,
Feb 28, 2011, 3:24:17 AM2/28/11
to google-map...@googlegroups.com, Silver

Silverio Lora Milanes

unread,
Feb 28, 2011, 11:49:16 AM2/28/11
to Chris Broadfoot, google-map...@googlegroups.com

Hello Chris, thanks by your answer but I think I didn't explain myself properly.

Currently the application is working property and I’m loading from MySQL the polygons information and I’m showing its in Google Maps, but… I have performance problems because I’m parsing with Javascript very long strings and I need to know about a better way for doing it.

My actual procedure is:

-          MySQL table with Geometry field (spatial extension)

-          Query it, using AsText(geometry) function

-          Send it to javascript in json format

-          Parse the string and separate each polygon’s node for creating an array of LatLng

-          Using google.maps.Polyline

Note:

I can’t use Postgres and PostGIS in my server.  

 

Some suggestion?

Chris Broadfoot

unread,
Feb 28, 2011, 4:51:31 PM2/28/11
to Silverio Lora Milanes, google-map...@googlegroups.com
Aha - perhaps some server-side processing would be good here.

If you could return the geometry as an array of floats, that would cut out most of your parsing time in JS.

I suggest you change your pipeline to:

-          MySQL table with Geometry field (spatial extension)
-          Query it, using AsText(geometry) function
-          Parse geometry into array of floats
-          Send it to javascript in json format
-          Create an array of LatLng
-          Using google.maps.Polyline

If that doesn't help, there may be some more improvements possible, such as storing a json representation alongside the MySQL geometry upon write.

Chris

Jason Sanford

unread,
Feb 28, 2011, 10:35:08 PM2/28/11
to google-map...@googlegroups.com
I'm not sure if mysql has the same st_asgeojson function as PostGIS but if it does, I've been working on a utility to convert GeoJSON objects (Point, LineString, Polygon) to Google Maps Vectors (Marker, Polyline, Polygon). This way you'd be dealing with native JavaScript objects instead of parsing a gigantic string.

--

ɑhmɑd ɑzimi

unread,
May 23, 2011, 2:54:39 AM5/23/11
to google-map...@googlegroups.com
Hi Jason,
cool library, but what about performance? Is it good idea to parse this kinds of data on clients?
Or is it better to do this on server?
Reply all
Reply to author
Forward
0 new messages