Create hexagonal grid over city and associate with lon / lat points (in R)

懵懂的女人 提交于 2021-02-09 08:33:32

问题


I've been researching this for a while now but haven't come across any solution that fit my needs or that I can transform sufficiently to work in my case:

I have a large car sharing data set for multiple cities in which I have the charging demand per location (e.g. row = carID, 55.63405, 12.58818, charging demand). I now would like to split the area over the city (example above is Copenhagen) up into a hexagonal grid and tag every parking location with an ID (e.g. row = carID, 55.63405, 12.58818, charging demand, cell ABC) so I know which hexagonal cell it belongs to.

So my question is twofold: (1) how can I create such a honeycomb grid with a side length of 124 meters (about 40000 sqm which is equivalent to 200x200 meters but nicer in hexagonal) in this area:

my_area <- structure(list(longitude = c(12.09980, 12.09980, 12.67843, 12.67843), 
                       latitude = c(55.55886, 55.78540, 55.55886, 55.78540)), 
                  .Names = c("longitude", "latitude"), 
                  class = "data.frame", row.names = c(NA, -4L))

(2) How can I then associate all of my points on the map with a certain grid cell?

I'm really lost at this point, I tried to use tons of packages like rgdal, hexbin, sp, raster, rgeos, rasterVis, dggridR, ... but none of them got me to where I want to go. Help is much appreciated!

Parking data example:

                  id latitude longitude           timestamp charging_demand
1: WBY1Z210X0V307780 55.68387  12.60167 2016-07-30 12:35:07              22
2: WBY1Z210X0V307780 55.63405  12.58818 2016-07-30 16:35:07              27
3: WBY1Z210X0V307780 55.68401  12.49015 2016-08-02 16:00:08              44
4: WBY1Z210X0V307780 55.68694  12.49146 2016-08-03 13:40:07               1
5: WBY1Z210X0V307780 55.68564  12.48824 2016-08-03 14:00:07              66
6: WBY1Z210X0V307780 55.66065  12.60569 2016-08-04 16:19:15              74

回答1:


I think you can indeed use the hexbin package. Call the function like this:

h <- hexbin(data_x, data_y, nbins, range_x, range_y, IDs = TRUE)

The result has a column cID which tells you the cell in which the observation falls. You can use this to e.g. calculate the average charging demand per cell:

tapply(charging_demand, h@cID, FUN = function(z) sum(z)/length(z))

Additionally you can use hcell2xy to get coordinates you can use for plotting with ggplot. For an example you can look at this answer.



来源:https://stackoverflow.com/questions/42893734/create-hexagonal-grid-over-city-and-associate-with-lon-lat-points-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!