Intro

Open source geo-spatial web based tools usually import file formats such as .geojson instead more conventional GIS formats, i.e. .shp and .dxf. Likewise Ravenview, a web-based viewer developed for visualizing a Raven model spatio-temporal parameters, uses .geojson file formats to view a watershed map. The current version of the viewer is able to visualize a Raven compliant .rvh (the subbasin(s) command block) and .rvt files. However, the viewer only accepts .geojson files as input. This document goes through a number of steps to convert an GIS file, i.e. ESRI .shp, to a .geojson file using the R language of statistical computations. The conversion of a subbasin geo-spatial file is not computationally expensive, however, the labeling of GIS file features should follow a standard Raven names convention. Accordingly, alongside an ESRI shapefile a Raven readable .rvh, the Raven watersheds/HRUs file equivalent to the shapefile, must be provided to the converter for further label and geo-spatial data checking.

Example

This section presents the utility of an R function to convert and simplify a Raven compliant shapefile to a .geojson file. The R function, rvn_rvh_shp_geojson and a river basin geo-spatial data are downloaded from a git repo. Then the geo-spatial data of the basin are converted to a .geojson format. The function arguments and its default values are as below:

rvn_rvh_shp_geojson(shpfile,
                    rvhfile,
                    outputfile=sprintf("%s/output.json",getwd()),
                    CRSshp=NA,
                    matchingColumns=list(shpfile="subid",rvhfile="subid"),
                    outletCoords=list(outletLat=NA,outletLng=NA),
                    simplifyGeometry=TRUE)

where,

Having understood all the arguments of the rvn_rvh_shp_geojson() described above, we will be ready to download and unzip the geo-spatial data sets using the code chunk below. It is important to keep in mind that, the basis of the accurate conversion is matchingColumns. User should select the column in the shape file that strictly is equal to the selected column in the rvh file, otherwise, the comparisons of files may fail or returns a geojson file with inaccurate geo-spatial specifications.

data_url<-"http://raven.uwaterloo.ca/files/JSON_converter_files.zip"
download.file(data_url,destfile = "JSON_converter_files.zip")
unzip(zipfile="JSON_converter_files.zip",exdir = "JSON_converter_files")
source("./JSON_converter_files/rvn_rvh_shp_geojson.R")

Having all datasets and the converter, the below code chunk will converts the sets into a .geojson file given that the shape file geometry is assumed to by simplified by default: ion:

# Liard River basin example
shpfile<-"./JSON_converter_files/Liard/subbasin_20180718.shp"
rvhfile<-"./JSON_converter_files/Liard/Liard.rvh"
matchingColumns<-list(shpfile="Sub_B",rvhfile="Name")
outputfile<-sprintf("%s/Liard.json",getwd())
rvn_rvh_shp_geojson(shpfile=shpfile,rvhfile=rvhfile,
                            matchingColumns=matchingColumns,
                            outputfile = outputfile)
## Warning in rgdal::readOGR(dirname(x), fn, stringsAsFactors = stringsAsFactors, :
## Z-dimension discarded
## Warning in rvn_rvh_shp_geojson(shpfile = shpfile, rvhfile = rvhfile,
## matchingColumns = matchingColumns, : Outlet coordinates were not provided. They
## were calculated by averaging the coordinates of HRUs within the outlet subbasin!
## Success! File is at E:/UW/geojson converter/Liard.json
## Successfully Converted!

You may disregard the warnings thrown by rgdal tools, however, the other reported warnings should not be neglected. For instance, since we are not pulling the function with a specified outlet coordinates, the routine tells us the outlet coordinates are computed by averaging coordinates of the HRUs, laying within the outlet subbasin.

We can also check a few more conversions. See below:

# Nith River basin example
shpfile<-"./JSON_converter_files/Nith/Nith_shapefile_sample.shp"
rvhfile<-"./JSON_converter_files/Nith/Nith.rvh"
matchingColumns<-list(shpfile="subID",rvhfile="SBID")
outputfile<-sprintf("%s/Nith.json",getwd())
rvn_rvh_shp_geojson(shpfile=shpfile,
                    rvhfile=rvhfile,
                    outputfile=outputfile,
                    matchingColumns=matchingColumns)
# Madawaska River basin example
shpfile<-"./JSON_converter_files/Madawaska/madawaska_subbasin.shp"
rvhfile<-"./JSON_converter_files/Madawaska/Madawaska.rvh"
matchingColumns<-list(shpfile="Subbasin",rvhfile="SBID")
outputfile<-sprintf("%s/Madawaska.json",getwd())
rvn_rvh_shp_geojson(shpfile=shpfile,
                    rvhfile=rvhfile,
                    outputfile = outputfile,
                    matchingColumns=matchingColumns)

References