SymbolixAU / googleway

R Package for accessing and plotting Google Maps
http://symbolixau.github.io/googleway/
Other
232 stars 46 forks source link

Draw order add_kml #163

Open mosscoder opened 6 years ago

mosscoder commented 6 years ago

Greetings, I would like to display a KML layer on top of a GeoJSON layer, however, my KML layer is drawn first (below) the GeoJSON layer, no matter which is added first.

library(googleway)
library(rgdal)
library(geojsonio)
library(geojsonlint)

tempZip <- tempfile()

download.file(url = 'http://www2.census.gov/geo/tiger/GENZ2017/shp/cb_2017_us_state_500k.zip',
                      destfile = tempZip)
unzip(tempZip)

statesPoly <- readOGR(path.expand(getwd()),
                      layer = 'cb_2017_us_state_500k')

cali <- subset(statesPoly, 
               NAME == "California")

cali$fillColor <- 'red'

caliGJ <- geojson_json(cali, 
                       geometry = 'polygon')

key <- 'key'

#KML added as the second layer, appears as if it was drawn first, however
google_map(key = key) %>%
  add_geojson(caliGJ) %>%
  add_kml(kml_url = 'http://www.energy.ca.gov/maps/renewable/BuildingClimateZonesMap.kmz',
          update_map_view = T)

#No different than geojson added as the second layer
google_map(key = key) %>%
  add_kml(kml_url = 'http://www.energy.ca.gov/maps/renewable/BuildingClimateZonesMap.kmz',
          update_map_view = T) %>%
  add_geojson(caliGJ) 
SymbolixAU commented 6 years ago

I'll take a look.

I've tried adding a z_index argument to the KML layer but it's not obeying it.

And a note about your code if I may:

I've written the geojsonsf package, which is quicker at parsing GeoJSON than geojsonio. Also, plotting GeoJSON through add_geojson() is slower than using an sf object in add_polygon() method, so I would recommend adding polygons rather than geojson if you can

library(googleway)
library(geojsonsf)
library(sf)  ## load this library if you want to 'print' the sf object to console

geo <- geojsonsf::geojson_sf("http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_500k.json")
## sf::st_read("http://eric.clst.org/...") is just as quick. 

## geo is now an `sf` object
cali <- geo[geo$NAME == "California", ]
set_key("GOOGLE_MAP_KEY")

google_map() %>%
  add_polygons(data = cali, z_index = 1, fill_opacity = 1) %>%
  add_kml(kml_url = 'http://www.energy.ca.gov/maps/renewable/BuildingClimateZonesMap.kmz',
          update_map_view = T, z_index = 2)
SymbolixAU commented 6 years ago

Reference: There is a zIndex property

But it doesn't appear to work, even when layering KMLs

google_map() %>%
  add_kml(kml_url = 'http://www.energy.ca.gov/maps/renewable/BuildingClimateZonesMap.kmz',
          update_map_view = T, z_index = 2, layer_id = "building") %>%
add_kml(kml_url = 'http://www.energy.ca.gov/maps/renewable/renewable_development.kmz',
        update_map_view = T, z_index = 1, layer_id = "renewable")
SymbolixAU commented 6 years ago

Have asked this question on SO. Apparently it should work...

SymbolixAU commented 6 years ago

I can confirm the z_index value does work with this example

google_map() %>%
  add_kml(
    kml_url = "http://www.geocodezip.com/geoxml3_test/kml/TopMarker.kml"
    , z_index = 2
    ) %>%
  add_kml(
    kml_url = "http://www.geocodezip.com/geoxml3_test/kml/MiddleSquare.kml"
    , z_index = 1
    ) %>%
  add_kml(
    kml_url = "http://www.geocodezip.com/geoxml3_test/kml/BottomParallelogram.kml"
    , z_index = 3
  )

as per one of the comments to the question. Maybe it has to do with the KML layers themselves?


Here's a screenshot of clicking the 'marker', but displaying the info window for the parallelogram (the highest z_index

screen shot 2018-05-25 at 8 14 02 am

SymbolixAU commented 6 years ago

I'm going to merge this into master (because the z_index is working), but keep the issue open

SymbolixAU commented 6 years ago

@mosscoder I don't suppose you have any extra insight as to why your KML layers aren't obeying the z_index property?