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 know a good source which fills 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();

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

// Presumably featureGroup is already instantiated and added to your map.

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

As of leaflet 1.5, wicket's .toObject() method isn't working out for me. It created a feature and got mapped, but I could not get any onEachFeature type action to work with it. But the .toJson() function converts to the geometry component of a standard geojson feature, and I took that forward.

var wkt_geom = "POINT(34.0218531, -81.0707438)";
var wkt = new Wkt.Wkt();
var feature = { "type": "Feature", 'properties': {}, "geometry": wkt.toJson() };

Now "feature" is a simple json with proper geojson syntax (and no functions, objects etc in it; just data!), and it can be loaded into leaflet/map using L.geoJson(feature). If you want metadata to go with your shape, you can assign it by:

feature.properties = { ... };

and after that do the L.geoJson(feature) declaration.

I've had a similar experience with turfJS : get the data down to the native json structure and then proceed, rather than trying to shove one library's object into another's. A few years on they usually diverge.

Your Answer

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

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