R rMaps geoJson function not plotting - r

I have a JSON route coming from ESRI service. I try to plot the route using geoJson using rMaps. The only output I get is the map but not the plot from the polygon.
This is the code:
require(rMaps)
json = "{\"messages\":[],\"routes\":{\"fieldAliases\":{\"ObjectID\":\"ObjectID\",\"Name\":\"Name\",\"FirstStopID\":\"FirstStopID\",\"LastStopID\":\"LastStopID\",\"StopCount\":\"StopCount\",\"Total_TravelTime\":\"Total_TravelTime\",\"Total_Kilometers\":\"Total_Kilometers\",\"Total_Miles\":\"Total_Miles\",\"Shape_Length\":\"Shape_Length\"},\"geometryType\":\"esriGeometryPolyline\",\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326},\"features\":[{\"attributes\":{\"ObjectID\":1,\"Name\":\"Location 1 - Location 2\",\"FirstStopID\":1,\"LastStopID\":2,\"StopCount\":2,\"Total_TravelTime\":2.8403004080455228,\"Total_Kilometers\":0.79761507718052971,\"Total_Miles\":0.49561503145413888,\"Shape_Length\":0.0082585488982182334},\"geometry\":{\"paths\":[[[-122.40774845199996,37.783745569000075],[-122.40745999999996,37.783510000000035],[-122.40722999999997,37.783340000000067],[-122.40718999999996,37.783310000000029],[-122.40648999999996,37.782740000000047],[-122.40591999999998,37.782300000000077],[-122.40520999999995,37.782870000000059],[-122.40442999999999,37.78350000000006],[-122.40432999999996,37.783580000000029],[-122.40372999999994,37.784070000000042],[-122.40329999999994,37.78374000000008],[-122.40269999999998,37.783270000000073],[-122.40411865599998,37.782150343000069]]]}}]},\"directions\":[{\"routeId\":1,\"routeName\":\"Location 1 - Location 2\",\"summary\":{\"totalLength\":0.49561503145413888,\"totalTime\":2.8403004049323499,\"totalDriveTime\":2.8403004080455228,\"envelope\":{\"xmin\":-122.40789999999998,\"ymin\":37.782000000000039,\"xmax\":-122.40269999999998,\"ymax\":37.784070000000042,\"spatialReference\":{\"wkid\":4326,\"latestWkid\":4326}}},\"features\":[{\"attributes\":{\"length\":0,\"time\":0,\"text\":\"Start at Location 1\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTDepart\"},\"compressedGeometry\":\"+1m91-6fki2+202vh+0+0\"},{\"attributes\":{\"length\":0.13919863031093585,\"time\":0.8808930174425843,\"text\":\"Go southeast on 5th St toward Stevenson St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTStraight\"},\"compressedGeometry\":\"+1m91-6fki2+202vh+36-2g\"},{\"attributes\":{\"length\":0.16963433548079213,\"time\":0.96497624999999998,\"text\":\"Turn left on Minna St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnLeft\"},\"compressedGeometry\":\"+1m91-6fkes+202t1+3q+33\"},{\"attributes\":{\"length\":0.07799196861297697,\"time\":0.51913014739269459,\"text\":\"Turn right on 4th St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnRight\"},\"compressedGeometry\":\"+1m91-6fkb2+20304+1p-1d\"},{\"attributes\":{\"length\":0.10879009704943393,\"time\":0.47530099321024388,\"text\":\"Turn right on Howard St\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTTurnRight\"},\"compressedGeometry\":\"+1m91-6fk99+202un-2f-1u\"},{\"attributes\":{\"length\":0,\"time\":0,\"text\":\"Finish at Location 2, on the left\",\"ETA\":-2209161600000,\"maneuverType\":\"esriDMTStop\"},\"compressedGeometry\":\"+1m91-6fkbo+202sp+0+0\"}]}]}"
regions=RJSONIO::fromJSON(json)
lmap <- Leaflet$new()
lmap$tileLayer(provide='Stamen.TonerLite')
lmap$setView(c(37.78356, -122.4079), 13)
lmap$geoJson(
regions)
legend_vec = c('red'='high', 'blue'='medium', 'green'='low')
lmap$legend(position = 'bottomright',
colors = names(legend_vec),
labels = as.vector(legend_vec))
lmap
Anyone knows why this JSON is not displayed in the map?
Thanks!

That's not a valid GeoJSON object. L.GeoJSON (Leaflet's GeoJSON layer) only accepts a valid GeoJSON featurecollection object, or an array with valid GeoJSON feature objects. This is a valid GeoJSON featurecollection object:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"myProperty": "myValue"
},
"geometry": {
"type": "Point",
"coordinates": [0,0]
}
},{
"type": "Feature",
"properties": {
"myProperty": "myValue"
},
"geometry": {
"type": "LineString",
"coordinates": [
[-45, -45],
[45, 45]
]
}
}]
}
You can learn more about GeoJSON here: http://geojson.org/ and you can validate GeoJSON objects here: http://geojsonlint.com/ For good measure here is also the reference for Leaflet's L.GeoJSON layer: http://leafletjs.com/reference.html#geojson and a tutorial from Leaflet on how to use L.GeoJSON layers: http://leafletjs.com/examples/geojson.html

Related

Cosmos DB query return points outside the polygon using ST_Within

Im using the query explorer in cosmosdb for searching points who are inside a polygon, my polygon in this sample is this (can be saw in geojson.io)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry":{
"type":"Polygon",
"coordinates":
[[[-107.44131585954042, 24.801824950217672],
[-107.43361255454303, 24.801824950217672],
[-107.43361255454303, 24.791345071112183],
[-107.44131585954042, 24.791345071112183],
[-107.44131585954042, 24.801824950217672]]]
},
"properties": {
"name": "The Polygon"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-107.437779, 24.798064]
},
"properties": {
"name": "The point inside the polygon"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-107.39355, 24.792837]
},
"properties": {
"name": "The point offside the polygon"
}
}
]
}
but when i search in the query explorer, cosmosdb retrieve me both points
This is my query
SELECT * FROM root
WHERE ST_Within(
root["Punto"], {"type": "Polygon", "coordinates":
[[[-107.44131585954042, 24.801824950217672],
[-107.43361255454303, 24.801824950217672],
[-107.43361255454303, 24.791345071112183],
[-107.44131585954042, 24.791345071112183],
[-107.44131585954042, 24.801824950217672]]]
})
root["Punto"] is a valid GeoJSON Point, i checked it with the ST_ISVALID function, and also, if i use the function ST_DISTANCE, checking if the distance between the point and the polygon is greater than zero, the data retrieved is correct, but i don't know if this approach is correct
This is my second query using ST_DISTANCE
SELECT root.NombreUbicacion, root.Punto
from root
where ST_DISTANCE (root.Punto, {"type": "Polygon", "coordinates":
[[[-107.44131585954042, 24.801824950217672],
[-107.43361255454303, 24.801824950217672],
[-107.43361255454303, 24.791345071112183],
[-107.44131585954042, 24.791345071112183],
[-107.44131585954042, 24.801824950217672]]]
}) > 0
I'm send a mail to askcosmosdb#microsoft.com and they told me that i need to rearrange the polygon
The behavior that you are seeing is because the points in the polygon are in the reverse order. If you look at the documentation
"Points within a Polygon must be specified in counter-clockwise order. A Polygon specified in clockwise order represents the inverse of the region within it."
So, i change the query to this, a it works like a charm
SELECT * FROM root
WHERE ST_Within(
root["Punto"], {"type": "Polygon", "coordinates":
[[[-107.44131585954042, 24.801824950217672],
[-107.44131585954042, 24.791345071112183],
[-107.43361255454303, 24.791345071112183],
[-107.43361255454303, 24.801824950217672],
[-107.44131585954042, 24.801824950217672]]]
})
Thanks to the cosmosdb team
Under "Spatial SQL built-in functions" section in this article, you will see:
Updated:
As you said, using the point {'type': 'Point', 'coordinates':[-107.437779, 24.798064]} it returns the distance.
But based on my test with many other valid GeoJSON Points, ST_DISTANCE (point_expr, polygon_expr) often return 0, so using ST_DISTANCE (point_expr, polygon_expr) in WHERE condition as a filter may not work as expected.
I can not find official documentations or blogs that explain the above issue, if possible, you can contact the team on askdocdb#microsoft.com to get details about ST_DISTANCE (spatial_expr, spatial_expr).

How to convert a point to polygon?

I have a big data in GeoJSON with Point type like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
2.8125,
47.040182144806664
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
14.765625,
48.69096039092549
]
}
}
]
}
As far as I know, in order to create polygon on a map, my GeoJSON should have Polygon as a Type instead of Points. How can I do it? Does anybody have idea? Should I convert them into polygon? How? I really need your help guys... (I am using Mapbox-Gl-Js by the way)
Thanks!
was having same issue searched for ages then found this lib Terraformer, (http://terraformer.io/core/#terraformercircle), one of the methods allows you to specify a long and lat point, and convert that into a circle around that point.
See my implementation below or you can also go through the docs
http://terraformer.io/core/#terraformercircle
//get the Terraformer lib
var Terraformer = require("terraformer");
const getPolygonFromPoints = async => {
//get the geoJSON
const getPointBasedGeoJSON = await require({localresource}); //or use a fetch if it is an external resource.
//loop over all the featues using ES6 map, to generate a new array of polygon based coordinates
const polygonBasedGeoJSON = getPointBasedGeoJSON.features.map(d => ({
type: "Feature",
...new Terraformer.Circle(
[d.geometry.coordinates[0], d.geometry.coordinates[1]],
500, //The radius of the circle in meters.
5 //How many steps will be used to create the polygon that represents the circle. i.e number of poins in the polyon array
),
/*
Terraformer.Circle will retrun an object similar to this, which we need to spread into our parent object, hence the ES6 spread
"geometry": {
"type": "Point",
"coordinates": [array of polygon coordinates based on the point you referenced]
},
*/
point: [d.geometry.coordinates[0], d.geometry.coordinates[1]], //so that you can still reference a point
properties: {
//specify any properties
}
}));
if (polygonBasedGeoJSON.length > 0){
return polygonBasedGeoJSON
}
return []
}
//Points to Polygon collection ready for use
console.log(getPolygonFromPoints())
It's simple: I had a data regarding terrorism in csv with latitude, longitude. When i converted it to geojson I had a structure like this:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
2.8125,
47.040182144806664
]
}
}
The question was: how I can create a map like this using this data: terrorism_map
I know that my geojson is useless in this case, but i need a way of creating map using this data. Sorry to confuse...

Is there an equivalent of the pointToLayer function in the R leaflet library

I am new to leaflet and R.
I am trying to create interactive maps with R (leaflet and shiny). I basically need to trace a route with points on a map. the data comes from a GeoJSON file with properties which need to be used for style options. An earlier implementation with leaflet in html looks like this:
L.geoJson(route2aug,{
style: function (feature) {
return feature.properties && feature.properties.style;
},
pointToLayer : function(feature,latlng){
var popupContent = "Sootmass:" + String(feature.properties.sootmass);
var popupOptions = {maxWidth: 200};
return L.circleMarker(latlng,{
radius: 4,
fillColor: getColor(feature.properties.sootmass),
color: getColor(feature.properties.sootmass),
weight: 1,
opacity: 1,
fillOpacity: 0.8
}).bindPopup(popupContent, popupOptions);
}
}).addTo(map);
Here,the data comes from the route2aug file which is imported as a js object. I need to replicate this in R with leaflet.
Is there a way to do the same thing in R?
The GeoJSON data file looks like this:
{"geometry": {"type": "Point", "coordinates": [77.68137666666667, 12.926686666666667]}, "type": "Feature", "properties": {"sootmass": 5}},{"geometry": {"type": "Point", "coordinates": [77.68138666666667, 12.926686666666667]}, "type": "Feature", "properties": {"sootmass": 10}},{"geometry": {"type": "Point", "coordinates": [77.681405, 12.926688333333335]}, "type": "Feature", "properties": {"sootmass": 15}}.......}

Document DB Polygon Query

My previous question is here DocumentDB Spatial Query - Odd Result
Now the query below won't return the document.
SELECT * FROM root r WHERE ST_WITHIN({'type':'Point','coordinates':[3.5, 8.2]}, r.geometry)
This is my amended document:
{
"id": "747941cfb829_1453640096710",
"geometry": {
"type": "Polygon",
"coordinates": [[[0,
0],
[10,
0],
[10,
10],
[0,
0]]]
},
"name": "name"}
How do I return this document? Thanks in advance
Your polygon is a triangle and the point you specify in your query is outside of that triangle. Try [8.2, 3.5].

How to get the Bounding box values from a GeoJSON file?

I have of GeoJSON file. But problem is, I do not know the projection of this file. Now, I want to overlay on my base map(OSM). How should I do it? This GeoJSON file contains features around Stockholm area. How can i set the bounding box for this? I tried with couple of openlayers example from hosting sites. But, those do not work for me. I got one example which is very similar with what i want, from (http://dev.openlayers.org/releases/OpenLayers-2.11/examples/all-overlays.html). I checked the source code he used bounding box. But problem is, if I remove bounding box this example does not work(I tied with given data with this). I do not know, is there anything wrong with my json file? Bellow, I have given some sample data. Is the format OK? As I told, I tried with different way. Some time i read the json file just fine from script. But, It can not show the featured overlay ed map. Bellow, I gave sample code, that shows how I tried.
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "FNODE_": 387603, "TNODE_": 387603, "LPOLY_": 0, "RPOLY_": 0, "LENGTH": 89.206900, "ROADS_SWER": 519006, "ROADS_SW_1": 519006, "REF": "", "TYPE": "residential", "ONEWAY": 8224, "BRIDGE": 8224, "MAXSPEED": 0, "LENGTH_M": 89.500000, "ID": 0, "dist_fnode": 13655.200000, "dist_tnode": 13655.200000 }, "geometry": { "type": "LineString", "coordinates": [ [ 676868.875000, 6569872.000000 ], [ 676882.437500, 6569861.000000 ], [ 676894.062500, 6569851.500000 ], [ 676894.500000, 6569844.500000 ], [ 676891.812500, 6569840.500000 ], [ 676887.312500, 6569841.000000 ], [ 676882.187500, 6569843.000000 ], [ 676875.250000, 6569850.000000 ], [ 676868.125000, 6569858.500000 ], [ 676866.500000, 6569867.000000 ], [ 676868.875000, 6569872.000000 ] ] } }
,
{ "type": "Feature", "properties": { "FNODE_": 387723, "TNODE_": 387724, "LPOLY_": 0, "RPOLY_": 0, "LENGTH": 9.581310, "ROADS_SWER": 519163, "ROADS_SW_1": 519163, "REF": "", "TYPE": "service", "ONEWAY": 8224, "BRIDGE": 8224, "MAXSPEED": 0, "LENGTH_M": 9.500000, "ID": 1, "dist_fnode": 13705.100000, "dist_tnode": 13695.600000 }, "geometry": { "type": "LineString", "coordinates": [ [ 677125.375000, 6569906.500000 ], [ 677133.437500, 6569911.500000 ] ] } }
,
..
..
]
}
Code Sample----
//OSM Layer-----------------------------------------------
var layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
//GeoJSON Layer-------------------------------------------
var vector_format = new OpenLayers.Format.GeoJSON({});
var vector_protocol = new OpenLayers.Protocol.HTTP({
url: 'ml/roads.json',
format: vector_format
});
var vector_strategies = [new OpenLayers.Strategy.Fixed()];
var vector_layer = new OpenLayers.Layer.Vector('More Advanced Vector Layer',{
protocol: vector_protocol,
strategies: vector_strategies,
isBaseLayer: false
});
var options = {
layers: [layer, vector_layer]
};
var map = new OpenLayers.Map("map", options);
//Projection-----------------------------------------------
map.setCenter(
new OpenLayers.LonLat(18.068611, 59.329444).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 10
);
Thanks, in advance.
vector_layer.events.on({
loadend: function() {
map.zoomToExtent(vector_layer.getDataExtent());
}
});
I still think you should set your projection in the map constructor…

Resources