I'm fairly new to handling spatial data and leaflet in general. I'm having difficulty creating a popup for my map. Basically what I want in the popup is the coordinates of my polygon and one of the properties (the type of class). Below is an test example of my geoJSON file:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"class": "blah"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-81.7987060546875,
32.74570253945518
],
[
-81.6229248046875,
32.16631295696736
],
[
-80.958251953125,
32.4263401615464
],
[
-81.2713623046875,
32.791892438123696
],
[
-81.7437744140625,
32.97180377635759
],
[
-81.7987060546875,
32.74570253945518
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blah2"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-82.056884765625,
33.55512901742288
],
[
-81.4471435546875,
33.247875947924385
],
[
-81.40869140625,
33.80653802509606
],
[
-82.078857421875,
33.88865750124075
],
[
-82.40295410156249,
33.58716733904656
],
[
-82.056884765625,
33.55512901742288
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blahh3"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-83.485107421875,
32.930318199070534
],
[
-83.07861328125,
31.863562548378965
],
[
-82.21618652343749,
32.11049589629439
],
[
-82.97973632812499,
33.22030778968541
],
[
-83.726806640625,
33.211116472416855
],
[
-83.485107421875,
32.930318199070534
]
]
]
}
}
]
}
Here is the code I have so far to create my map, but I'm struggling/don't even know where to start on creating a popup that includes my coordinates and property:
blahTest <- geojson_read("/file/path/...", what = "sp")
fpal <- colorFactor("viridis", blahTest$class)
leaflet(blahTest) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
color = ~fpal(class)) %>%
addLegend(colors = ~fpal, opacity = 1.0, labels = ~fpal) %>%
addPopups()
Thanks in advance!
Overview
Using #patL's suggestion, I used the popup parameter within leaflet::addPolygon() function to add label both the class and coordinates that define each polygon within blahTest.
I saved your sample data as .gejson file and imported it using sf::read_sf() and produced the HTML table within the popup using the htmlTable package after reading How to add an html table to leaflet popup.
# load necessary package
library( htmlTable )
library( leaflet )
library( sf )
# load necessary data
blahTest <-
read_sf(
dsn = "test.geojson"
, layer = "OGRGeoJSON"
)
# map data values to colors
fpal <- colorFactor( palette = "viridis", domain = blahTest$class)
# create map
my.map <-
leaflet( data = blahTest ) %>%
addTiles() %>%
addPolygons( stroke = FALSE
, smoothFactor = 0.3
, fillOpacity = 1
, color = ~fpal( class )
, popup = paste0(
"<b>Class: </b>"
, blahTest$class
, "<br>"
, "<b>Coordinates: </b>"
, "<br>"
, lapply(
X = blahTest$geometry
, FUN = function( i )
htmlTable(
x = i[[1]]
, header = c( "Longitude", "Latitude" )
)
)
) ) %>%
addLegend( pal = fpal
, values = ~class
, labels = ~class
, title = "Legend"
, position = "topright"
, opacity = 1.0 )
# view map
my.map
# end of script #
Related
I want to get from a data.table like this
temp <- data.table(data = list(data.table(a = 1:2,b=1:2)), type = "A")
data
type
<data.table[2x2]>
A
to a JSON like this
{
"group":
{
"data": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "A"
}
}
The Problem is I always end up with an additional array "[" for group.
What I have tried is tidyr::nest and
temp2 <- temp[, list(group=list(.SD))]
jsonlite::toJSON(temp2,pretty = TRUE, auto_unbox = TRUE)
temp3 <- temp[, (list(group=list(as.list(.SD))))]
jsonlite::toJSON(temp3,pretty = TRUE, auto_unbox = TRUE)
Is there an "easy" solution for my problem?
Thanks
edit more complex example
temp <-
data.table(
id1 = 1:6,
id2 = c(rep("A", 2), rep("B", 2), rep("C", 2)),
data = rep(list(data.table(
a = 1:2, b = 1:2
)), 6),
type = "test"
)
nest1 <- temp[, list(list(.SD)),by=.(id1,id2)] %>% setnames("V1","group")
nest1[, type:="B"]
nest2 <- nest1[, list(list(.SD)),by=.(id2)] %>% setnames("V1","data")
nest2[, type:="C"]
nest3 <- nest2[, list(list(.SD)),by=.(id2)] %>% setnames("V1","group")
jsonlite::toJSON(nest3, pretty = TRUE)
desired output (shortend):
Group should only contain objects and no arrays
[
{
"id2": "A",
"group": {
"data": [
{
"id1": 1,
"group": {
"data": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "test"
},
"type": "B"
},
{
"id1": 2,
"group": {
"data": [
{
"a": 1,
"b": 1
},
{
"a": 2,
"b": 2
}
],
"type": "test"
},
"type": "B"
}
],
"type": "C"
}
},
{
"id2": "B",
"group": {
"data": [],
"type": "C"
}
}
]
We could use jq to do the unboxing as a post-processing step, since jsonlite doesn't seem to allow for this specific use case:
jsonlite::toJSON(nest3, pretty = TRUE) %>%
jqr::jq('walk(if type=="array" and length==1 then .[0] else . end)')
The jq bit is taken from jq ~ is there a better way to collapse single object arrays?
I'm working on a worldmap in node-red and I was able to create a polygon and a linestring. But I need the linestring to have no fill and cannot differentiate between the line color (stroke color) and the fill color (it always has fill, like it was a polygon).
I've tried this, but no luck:
"properties": {
"stroke": "#555555",
"stroke-width": 2,
"stroke-opacity": 1,
"fill": "#00f900",
"fill-opacity": 0.5
},
This is where I declare the linestring:
var geo = { "type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[-1.356221,51.048611],
[-1.356039,51.048672],
[-1.355765,51.048311],
]
},
"properties": {
"color": "rgb(168, 5, 57)",
},
}]
}
The linestring is being presented on the map, but, besides the atual line, it also as a background on the same color as the line, like if it was a polygon.
fill-color : none
it should be find
I am following: http://radacad.com/create-custom-visual-with-r-and-json-part3
Here it is mentioned that with below R code, we can plot the chart:
Values<-data.frame(X,Y,Legend,Row,Column)
g=ggplot(Values, aes(x=X, y=Y,colour = Legend))
I am using similar implementations.
Below code renders blank chart with only axis:
Values <- data.frame(mpg_test, cyl_test)
p <- plot_ly(Values,
x = ~mpg_test,
y = ~cyl_test,
name = "SF Zoo",
type = "bar"
)
Empty chart
Below code also renders empty chart:
Values <- data.frame(mpg_test, cyl_test)
p <- plot_ly(Values,
x = mpg_test,
y = cyl_test,
name = "SF Zoo",
type = "bar"
)
Below code renders correct chart:
Values <- data.frame(mpg_test, cyl_test)
p <- plot_ly(Values,
x = ~mpg_test[1:nrow(Values), ],
y = ~cyl_test[1:nrow(Values), ],
name = "SF Zoo",
type = "bar"
)
Correct chart:
My doubt is, as per the tutorial I must get columns in mpg_test and cyl_test, then why I am getting something like dataframe in here.
Does, plotly handles things differently with the usage of ~ ?
Please help. Also, is there any way I can use the mpg_test and cyl_test without nrow(Values) inside plot_ly
script.r
source('./r_files/flatten_HTML.r')
############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
####################################################
################### Actual code ####################
Values <- data.frame(mpt_test, cyl_test)
p <- plot_ly(Values,
x = ~mpt_test[1:nrow(Values), ],
y = ~cyl_test[1:nrow(Values), ],
name = "SF Zoo",
type = "bar"
)
####################################################
############# Create and save widget ###############
#p = ggplotly(p);
internalSaveWidget(p, 'out.html');
####################################################
capabilities.json
{
"dataRoles": [
{
"displayName": "Mileage Per Gallon",
"kind": "GroupingOrMeasure",
"name": "mpg_test"
},
{
"displayName": "Cylinder Size",
"kind": "GroupingOrMeasure",
"name": "cyl_test"
}
],
"dataViewMappings": [
{ "conditions": [
{
"mpg_test": {
"max": 1
},
"cyl_test": {
"max": 1
}
}
],
"scriptResult": {
"dataInput": {
"table": {
"rows": {
"select": [
{
"for": {
"in": "mpg_test"
}
},
{
"for": {
"in": "cyl_test"
}
}
],
"dataReductionAlgorithm": {
"top": {}
}
}
}
},
"script": {
"scriptProviderDefault": "R",
"scriptOutputType": "html",
"source": {
"objectName": "rcv_script",
"propertyName": "source"
},
"provider": {
"objectName": "rcv_script",
"propertyName": "provider"
}
}
}
}
],
"objects": {
"rcv_script": {
"properties": {
"provider": {
"type": {
"text": true
}
},
"source": {
"type": {
"scripting": {
"source": true
}
}
}
}
}
},
"suppressDefaultTitle": true
}
I'm trying to create a leaflet plot with each geographical area a specific color based on a property. Here is an test example of my geojson file:
I'm pretty new to geojson and leaflet in general
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"class": "blah"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-81.7987060546875,
32.74570253945518
],
[
-81.6229248046875,
32.16631295696736
],
[
-80.958251953125,
32.4263401615464
],
[
-81.2713623046875,
32.791892438123696
],
[
-81.7437744140625,
32.97180377635759
],
[
-81.7987060546875,
32.74570253945518
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blah2"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-82.056884765625,
33.55512901742288
],
[
-81.4471435546875,
33.247875947924385
],
[
-81.40869140625,
33.80653802509606
],
[
-82.078857421875,
33.88865750124075
],
[
-82.40295410156249,
33.58716733904656
],
[
-82.056884765625,
33.55512901742288
]
]
]
}
},
{
"type": "Feature",
"properties": {"class": "blahh3"},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-83.485107421875,
32.930318199070534
],
[
-83.07861328125,
31.863562548378965
],
[
-82.21618652343749,
32.11049589629439
],
[
-82.97973632812499,
33.22030778968541
],
[
-83.726806640625,
33.211116472416855
],
[
-83.485107421875,
32.930318199070534
]
]
]
}
}
]
}
What I want to do is add a legend based off the class properties, such that each different class is associated with a specific color. However, when I try and plot and add the legend I get an error on the plot. Below is my R code:
blahTest <- geojson_read("/file/path/...", what = "sp")
fpal <- colorFactor("viridis", blahTest$class)
leaflet(blahTest) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
color = ~fpal(class)) %>%
addLegend(colors = ~fpal, opacity = 1.0, labels = ~fpal)
Just some slight modifications to your addLegend argument gives the required output:
# read geojson in r
library(rgdal)
blahTest = readOGR("...", "OGRGeoJSON")
library(leaflet)
# create palette
fpal <- colorFactor(
palette = "viridis",
domain = blahTest$class
)
# plot
leaflet(blahTest) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
color = ~pal(class)) %>%
addLegend("bottomright",
pal = fpal,
values = ~class,
title = "Legend",
opacity = 1,
labels = ~fpal
)
Is anyone aware of an existing grammar definition of Mscgen syntax that will work with PegKit? I had a look in the "res" folder but most of those don't seem to work.
Here is a sample
# MSC for some fictional process
msc {
hscale = "2";
a,b,c;
a->b [ label = "ab()" ] ;
b->c [ label = "bc(TRUE)"];
c=>c [ label = "process(1)" ];
c=>c [ label = "process(2)" ];
...;
c=>c [ label = "process(n)" ];
c=>c [ label = "process(END)" ];
a<<=c [ label = "callback()"];
--- [ label = "If more to run", ID="*" ];
a->a [ label = "next()"];
a->c [ label = "ac1()\nac2()"];
b<-c [ label = "cb(TRUE)"];
b->b [ label = "stalled(...)"];
a<-b [ label = "ab() = FALSE"];
}