I have a Leaflet map and want to add two of my own small polygon layers (currently in shapefile format, but they could be converted to GeoJSON). Can this be done without a mapserver to publish them as map services? Is so, are there specific instructions for this?

If it can not be done, are there "free" services for publishing small map layers?

  • Do you want to use a mapserver? You would not need one necessarily. Leaflet can render GeoJSON in Javascript in the browser. – underdark Mar 31 '13 at 22:53
  • my question is I set in mapserver wms service as seen in the file at the same time set up the index file with the call to the web service and doubles as seen in the photo, I tested this web service from first and works without problems, let me know because it comes so thanks The map file, index.html and image output map are here www.dapboyaca.gov.co/tools/Mapa.zip – Christiam Niño Aug 28 '13 at 2:13

Convert your shapefile to geojson. I use QGIS and save as geojson. Make sure you pick the WGS84/EPSG:4326 projection before saving.

Then use this plugin to call the geojson layer. leaflet-ajax Or you can follow the Leaflet tutorial for geojson

  • 1
    The above suggestion is a way to get a map published quickly. Keep in mind, the larger the geojson file the slower it'll take to render. Try to limit the number of features to 100 unless of course they're points. Plus you should remove attributes which you don't plan on using. – geomajor56 Apr 4 '13 at 19:37

rgeos provides functions that will let you do an end-around.

This plots a grayed-out leaflet map of NYC:

 leaflet() %>% 
   addTiles(urlTemplate = "http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png", attribution = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>') %>% 
   setView(-73.97125, 40.78306, zoom=10) 

However, it's hard to pick-out the city from the rest of the metro area. One way to do so is to make a polygon from the city boundaries, and then add that to the map in a darker color.

 #NYC is a shapefile of NYC city boundaries
 test <- gIntersection(NYC, NYC, byid=FALSE) # create a spatial polygon by intersecting the city with itself
 test <- spTransform(test, CRS("+proj=longlat +datum=WGS84")) # force the polygon to the same crs as leaflet maps

All that is left to do is use "addPolygons" to put "test" on the original map.

 leaflet() %>% 
 addTiles(urlTemplate = "http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png", attribution = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>') %>% 
 setView(-73.97125, 40.78306, zoom=10) %>% 
 addPolygons(data=test, weight = 3, color = "black")

There may be more elegant ways to transform the shapefile to a polygon, but from a practical standpoint, this approach is pretty simple. Hope it helps.


it doesn't work for me, I can validate the GeoJSON with the online tool. Here is the GeoJson I get:

"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "FID": 0.000000 }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 8.9777585, 45.661008099999982 ] ] } },
{ "type": "Feature", "properties": { "FID": 1.000000 }, "geometry": { "type": "MultiPoint", "coordinates": [ [ 8.9466397, 45.622191399999984 ] ] } }

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.