R、GeoJSON 和 Leaflet

2024-06-26

我最近从 R-Bloggers.com 的帖子中了解了 leafletjs.com。我想要实现的一个此类教程是使用传单创建交互式分区统计图(http://leafletjs.com/examples/choropleth.html http://leafletjs.com/examples/choropleth.html)。我一直在使用 R 的 rjson 包来创建由传单读取的 data.js 文件。尽管我已成功使用提供的形状文件作为传单中的可读 JSON 文件,但在尝试将数据框(“data.csv”)中的其他属性合并到 JSON 文件时,我无法重复该过程;在本例中,我使用 rGIS 来附加数据框中列出的每所学校的罐头数量数据。我希望实现的是在传单中创建一个分区统计图,显示高中区(由 NAME 变量标识)以及“罐头”的总和。我认为问题在于 writeOGR 将信息导出为点,而不是多边形?

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

    ###load R scripts from dropbox
    dropbox.eval <- function(x, noeval=F) {
    require(RCurl)
    intext <- getURL(paste0("https://dl.dropboxusercontent.com/",x), ssl.verifypeer = FALSE)
    intext <- gsub("\r","", intext)
    if (!noeval) eval(parse(text = intext), envir= .GlobalEnv)
    return(intext)
    }

    ##pull scripts from dropbox 
    dropbox.eval("s/wgb3vtd9qfc9br9/pkg.load.r")    
    dropbox.eval("s/tf4ni48hf6oh2ou/dropbox.r")

    ##load packages
    pkg.load(c(ggplot2,plyr,gdata,sp,maptools,rgdal,reshape2,rjson))

    ###setup data frames
    dl_from_dropbox("data.csv","dx3qrcexmi9kagx")
    data<-read.csv(file='data.csv',header=TRUE)

    ###prepare GIS shape and data for plotting
    dropbox.eval("s/y2jsx3dditjucxu/dlshape.r")     
    temp <- tempfile()
    dlshape(shploc="http://files.hawaii.gov/dbedt/op/gis/data/highdist_n83.shp.zip", temp)
    shape<- readOGR(".","highdist_n83") #HDOE high school districts  
    shape@proj4string 

    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 <- as.data.frame(shape2)

    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")

然而,我的特征似乎不断重复。如何聚合功能信息,使其看起来更像以下内容:

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],

我最终解决了这个问题。

我基本上所做的是将 data.2 df 加入到形状文件中:(shape2@data<-join(shape2@data,data.2)

然后使用 rgdal 包以 JSON 格式(使用 JSON 驱动程序)编写 OGR,扩展名为 *.js。

我希望这对其他人有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R、GeoJSON 和 Leaflet 的相关文章

随机推荐