jbaileyh / geogrid

Turning geospatial polygons into regular or hexagonal grids. For other similar functionality see the tilemaps package https://github.com/kaerosen/tilemaps
Other
395 stars 31 forks source link

Positions of some hexagons #14

Closed heon131 closed 6 years ago

heon131 commented 6 years ago

Hi, Joseph. First I'd like to thank you for your package; it is helping me a lot with my project.

I have a concern/question in creating a hexagonal grid for South Korea. Whenever I create hexagonal grids I see 3 to 6 different hexagon units generated in a district (island) which should technically have two geographic units. First I thought that maybe for each trial the positions of hexagons change, therefore, some of the hexagons move from the mainland to this island. Nevertheless... could you please explain what is possibly causing this or the mechanism? First I will show you my script and a basic plot of the shape file to show you what I mean.

install.packages("raster")

library(raster) kor2 <- getData(name = "GADM", country = "KOR", level = 2)

str(kor2, max.level = 2) Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots ..@ data :'data.frame': 229 obs. of 15 variables: ..@ polygons :List of 229 ..@ plotOrder : int [1:229] 62 64 108 66 117 56 65 121 67 123 ... ..@ bbox : num [1:2, 1:2] 125.1 33.1 130.9 38.6 .. ..- attr(*, "dimnames")=List of 2 ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

plot the shape file

plot(kor2)

image

As you can see, there are only two geographic units in Jeju island in the image above, the big island at the bottom, However, whenever I generate a set of hexagonal grids, different numbers of hexagons come out for that island. See below for my script and six hexagonal grids generated as a result.

basic setup for hexgird

library(devtools) library(hexmapr) original_shapes <- getData(name = "GADM", country = "KOR", level = 2) original_details <- get_shape_details(original_shapes)

generating 6 different hexagonal plots

par(mfrow=c(2,3), mar = c(0,0,2,0)) for (i in 1:6){ new_cells <- calculate_cell_size(original_shapes, original_details,0.03, 'hexagonal', i) plot(new_cells[[2]], main = paste("Seed",i, sep=" ")) }

image

Any ideas on why this is the case? Your feedback will be much appreciated!

sassalley commented 6 years ago

Hi @heon131 thanks for using the package and for providing a detailed issue.

The issue that you are facing probably relates to how the library generates the hexagonal grid. To generate a grid calculate_cell_size takes the boundaries of the shape (in this case South Korea) and determines how large the cells should be so that the correct number of cells fit into the boundaries of the shape.

Because the island off the Southern part of South Korea is quite large, there are probably many solutions where more than two cells fit into the 'area' of the island. This is actually a very interesting issue - thank you for raising it.

For now, i'd suggest running something like:

for (i in 1:20){
new_cells <- calculate_cell_size(original_shapes, original_details,0.03, 'hexagonal', i)
plot(new_cells[[2]], main = paste("Seed",i, sep=" "))
}

to see if different seed values yield any scenario where the island only has two hexagons. If that doesn't work, then perhaps try changing the learning rate to 0.05 or even 0.02.

If this still does not work then please let me know - it may be a great case for an extension to the package.

Best,

Joseph

sassalley commented 6 years ago

I suggested 1:20 as an example but trying up to 100 seed values may help.

Also, just so you are aware - the assign_polygons function may take a while with this many hexagons. It may seem like it is 'hanging' but it is probably working.

heon131 commented 6 years ago

Hi! Thanks for your answer. It definitely helped.

Changing the seed value and learning rate solved the problem; doing so did show some grids with only two hexagons for that specific island.

par(mfrow=c(3,4), mar = c(0,0,2,0))
for (i in 1:30){
  new_cells <-  calculate_cell_size(original_shapes, original_details,0.05, 'hexagonal', i)
  plot(new_cells[[2]], main = paste("Seed",i, sep=" "))
}

image

Thanks for your help! I appreciate it.