I have a point type WKT geometry column in a MySQL table storing the location of some stores.

I want to display it on a leaflet map on my website. I've found plenty of tutorials to doing this on leaflet official website from GeoJSON data.

But I found no precise documentation which clearly explains how to convert WKT to GeoJSON...even if it seems to be a very important part of the process.

Does someone knows a good source which fill this gap or can explain to me how to do this in PHP or javascript ?

If you've already got WKT, then you might considering using the JavaScript library, Wicket, to go straight from your WKT to Leaflet features. As this example shows, you can pass in a WKT string and a style/options object, and Wicket will return a feature object you can attach directly to a Leaflet FeatureGroup, etc.

This example assumes two things: 1) You have an existing layer or featureGroup already and instantiated and added to your map, and 2) you have an existing icon style for your markers. In this case I just assumed the greenIcon marker style from the official Leaflet documentation.

var wkt_geom = "POINT(34.0218531, -81.0707438)";
var wicket = new Wkt.Wkt();
wicket.read(wkt_geom);

// "greenIcon from official documentation noted above.
var feature = wicket.toObject({icon: greenIcon});

// Presumably featureGroup is already instantiated and added to your map.
featureGroup.addLayer(feature);

In addition to Wicket, there is also Wellknown

// With Leaflet or MapBox.js
var geojsonLayer = L.geoJson(parse('Point(1 2)'));
  • This method seems to be easier, but needs node.js. Is it necessary to dive into node.js to run it ? I'm a beginner programmer and don't know if it's a good idea to go in node.js while being still learning javascript... – Samy-DT Sep 15 '15 at 10:59
  • This is intriguing, but I dislike the additional dependencies. – elrobis Sep 15 '15 at 14:08
  • @elrobis, there are no dependencies and you don't need Node (unless you want to run on the command line). For use in browser, you just include this: raw.github.com/mapbox/wellknown/master/wellknown.js – toms Sep 15 '15 at 16:33
  • @toms are you certain? The usage notes state and in browsers via browserify or a standalone package. I confess that I'm not sure what that means. So you're saying wellknown.js can be used as-is, without any additional libs or require() calls? – elrobis Sep 15 '15 at 18:13
  • @elrobis I was wrong. I took the documentation at face value and assumed that "stand alone" meant that wellknown.js would ... stand alone. But just tested and it does not. So you are correct, it requires node or packaging using something like browserify. Apologies for the giving bad advice – toms Sep 15 '15 at 20:02

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.