Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently learned about from an post. One such tutorial that I would like to achieve is to create interactive choropleth maps with leaflet.

I have been using the rjson package for R to create the data.js file to be read by leaflet. Although I have had success with using the provided shape file as a readable JSON file in leaflet, I am unable to repeat the process when trying to merge additional properties from the data frame ("data.csv") to the JSON file. In this case, I have done rGIS to attach data on the number of cans in each school listed in the data frame.

What I would please like to achieve is to create a choropleth map in leaflet that displayes high school district (as identified by the NAME variable), and the sum of "cans". The issue, I believe, is that writeOGR exports the information as points, rather than polygon?

    "type": "Feature",
    "properties": {
        "name": "Alabama",
        "density": 94.65
    "geometry": ...

    ###load R scripts from dropbox
    dropbox.eval <- function(x, noeval=F) {
    intext <- getURL(paste0("",x), ssl.verifypeer = FALSE)
    intext <- gsub("\r","", intext)
    if (!noeval) eval(parse(text = intext), envir= .GlobalEnv)

    ##pull scripts from dropbox 

    ##load packages

    ###setup data frames

    ###prepare GIS shape and data for plotting
    temp <- tempfile()
    dlshape(shploc="", temp)
    shape<- readOGR(".","highdist_n83") #HDOE high school districts  

    shape2<- spTransform(shape, CRS("+proj=longlat +datum=NAD83"))

    data.2<-ddply(data, .(year, schoolcode, longitude, latitude,NAME,HDist,SDist), summarise,
            total = sum(total),
            cans= sum(cans))

    ###merging back shape properties and data frame
    coordinates(data.2) <-~longitude + latitude
    shape2@data$id <- rownames(shape2@data)

    sh.df <-

    sh.fort <- fortify(shape2 , region = "id" )

    sh.line<- join(sh.fort, sh.df , by = "id" )

    mapdf <- merge( sh.line , data.2 , by.x= "NAME", by.y="NAME" , all=TRUE)

    mapdf <- mapdf[ order( mapdf$order ) , ]

    ###exporting merged data frame as JSON
mapdf.sp <- mapdf
coordinates(mapdf.sp) <- c("long", "lat")
writeOGR(mapdf.sp, "hssra.geojson","mapdf", driver = "GeoJSON")

However, it appears that my features are repeating itself constantly. How can I aggregate the features information so that it looks more like the following:

var statesData = {"type":"FeatureCollection","features":[
{"type":"Feature","id":"01","properties":{"name":"Alabama","density":94.65},"geometry":    {"type":"Polygon","coordinates":[[[-87.359296,35.00118],[-85.606675,34.984749],  [-85.431413,34.124869],[-85.184951,32.859696],[-85.069935,32.580372],[-84.960397,32.421541],[-85.004212,32.322956],[-84.889196,32.262709],[-85.058981,32.13674],[-85.053504,32.01077],[-85.141136,31.840985],[-85.042551,31.539753],[-85.113751,31.27686],[-85.004212,31.003013],[-85.497137,30.997536],[-87.600282,30.997536],[-87.633143,30.86609],[-87.408589,30.674397],[-87.446927,30.510088],[-87.37025,30.427934],[-87.518128,30.280057],[-87.655051,30.247195],[-87.90699,30.411504],[-87.934375,30.657966],[-88.011052,30.685351],[-88.10416,30.499135],[-88.137022,30.318396],[-88.394438,30.367688],[-88.471115,31.895754],[-88.241084,33.796253],[-88.098683,34.891641],[-88.202745,34.995703],[-87.359296,35.00118]]]}},

{"type":"Feature","id":"02","properties":{"name":"Alaska","density":1.264},"geometry":   {"type":"MultiPolygon","coordinates":[[[[-131.602021,55.117982],[-131.569159,55.28229],[-131.355558,55.183705],[-131.38842,55.01392],[-131.645836,55.035827],[-131.602021,55.117982]]],[[[-131.832052,55.42469],[-131.645836,55.304197],[-131.749898,55.128935],[-131.832052,55.189182],[-131.832052,55.42469]]],[[[-132.976733,56.437924],[-132.735747,56.459832],[-132.631685,56.421493],[-132.664547,56.273616],[-132.878148,56.240754],[-133.069841,56.333862],[-132.976733,56.437924]]],[[[-133.595627,56.350293],
share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.