Invalid GeoJSON for polygon - json

I am trying to plot a polygon on a google map using geojson. Here is the PHP code where I am trying to build the polygon using four bounds values return from the query result array:
$arr3_poly = Array(
"type" => "Polygon",
"coordinates" => Array()
);
foreach ($q->result_array() as $row) {
$arr3_poly["coordinates"][] = Array(
floatval($row['v3_bounds_sw_lat']),
floatval($row['v3_bounds_sw_lng']),
floatval($row['v3_bounds_ne_lat']),
floatval($row['v3_bounds_ne_lng']),
);
}
When I then do json_encode($arr3_poly, JSON_PRETTY_PRINT);, this is the resulting output:
{
"type": "Polygon",
"coordinates": [
[
43.8526377,
-79.0499898,
43.8526509,
-79.0499877
],
[
43.8526546,
-79.0501977,
43.8526678,
-79.0501957
],
[
43.8526716,
-79.0504057,
43.8526848,
-79.0504037
]
]
}
There must be something wrong with this geojson because when I try to validate it at geojsonlint.com it returns with this error saying Failed to validate field 'coordinates' list schema.:
Any ideas what I am doing wrong? Thanks.

This works for me in geojsonlint.com (changed your points slightly to make it not look like a straight line):
{
"type": "Polygon",
"coordinates": [
[
[43.8526377,-79.0499898],
[43.854,-79.051],
[43.8526716,-79.0504057],
[43.8526377,-79.0499898]
]
]
}
However, looking closer at that map, they are in Antarctica, you probably wanted this, which is in Canada, near Toronto:
{
"type": "Polygon",
"coordinates": [
[
[-79.0499898,43.8526377],
[-79.051,43.854],
[-79.0504057,43.8526716],
[-79.0499898,43.8526377]
]
]
}
GeoJSON coordinates:
The order of elements must follow x, y, z order (longitude, latitude, altitude for
coordinates in a geographic coordinate reference system).
Which is the opposite order from a google.maps.LatLng object (that is Latitude, Longitude).

Related

Snowflake Get MIN/MAX values from Nested JSON

I have the following sample structure in snowflake column, indicating a series of coordinates for a map polygon. Each pair of values is formed of "longitude, latitude" like this:
COLUMN ZONE
{
"coordinates": [
[
[
-58.467372,
-34.557908
],
[
-58.457565,
-34.569341
],
[
-58.446836,
-34.573511
],
[
-58.43482,
-34.553367
],
[
-58.441944,
-34.547923
]
]
],
"type": "POLYGON"
}
I need to get the smallest longitud and the smallest latitude and the biggest longitud and latitude for every table row.
So, for this example, I need to get something like:
MIN: -58.467372,-34.573511
MAX: -58.43482,-34.547923
Do you guys know if this is possible using a query?
I got as far as to navigate the json to get the sets of coordinates, but I'm not sure how to proceed from there. I tried doing a MIN to the coordinates column, but I'm not sure how to reference only the "latitude" or "longitude" value.
This obviously doesn't work:
MIN(ZONE['coordinates'][0])
Any suggestions?
You can do this with some Snowflake GIS functions, and massaging the input data for an easier parsing:
with data as (
select '
{
"coordinates": [
[
[
-58.467372,
-34.557908
],
[
-58.457565,
-34.569341
],
[
-58.446836,
-34.573511
],
[
-58.43482,
-34.553367
],
[
-58.441944,
-34.547923
]
]
],
"type": "POLYGON"
}
' x
)
select st_xmin(g), st_xmax(g), st_ymin(g), st_ymax(g)
from (
select to_geography(replace(x, 'POLYGON', 'MultiLineString')) g
from data
)

How to flip lines and change keys in JQ

I'm creating a interactive map of my campus, the ideia is to replicate what I did on uMaps, in this link. The geojson was downloaded from UMap and I'm using the coordinates that came with it.
My first issue is my coordinates in the json, originally were a GeoJson, are sorted wrongly, my long came first then lat, thus when parse Google Maps can't read properly.
Json:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Almoxarifado / Patrimônio"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-52.163317,
-32.075462
],
[
-52.163884,
-32.075467
],
[
-52.163883,
-32.075336
],
[
-52.163321,
-32.075332
],
[
-52.163317,
-32.075462
]
]
]
}
},
{
...
},
{
...
},
...
]
}
So, I have to flip the coordinates lines to proper put in my Google Maps Api.
And my second issue is chaging the "type" key to "layer", for a better sepation layers in my app.
I've tried:
.features[] | .["type"] |= .["new value"]
How ever that changes the value and only accepts float values
Any help, advice or guidance would be greatly appreciated.
Part 1
flip the coordinates lines
For clarity and ease of testing, let's define a helper function:
# input: a JSON object with a coordinates array of arrays of pairs
def flip: .coordinates |= map(map(reverse)) ;
or even better - before invoking reverse, check the array has the expected length, e.g.:
def flip:
.coordinates
|= map(map(if length == 2 then reverse
else error("incorrect length: \(length)")
end)) ;
To flip the coordinates, we can now simply write:
.features[].geometry |= flip
Part 2
change the "type" key to "layer"
{layer: .type} + .
| del(.type)
Putting it together
{layer:.type} + .
| del(.type)
| .features[].geometry |= flip

Merging JSON with array to create new file

I am trying to make a map of the U.S. with Mapbox that shows median home price by county. I have a .json file that contains all the counties and is already accepted by Mapbox tileset -
{
"type": "Topology",
"transform": {
"scale": [
0.035896170617061705,
0.005347309530953095
],
"translate": [
-179.14734,
17.884813
]
},
"objects": {
"us_counties_20m": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01001"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01009"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01017"
},
{
"type": "Polygon",
"arcs": [],
"id": "0500000US01021"
}
]
}
}
}
Basically, it's a json file with "type" (Polygon), "arcs" (to map the county), and "id", which is an ID for the county.
This is great and accepted by Mapbox Tilesets to give me a visualization by county, but I need to add in median home price by county (in order to get colors by county, based on price).
I have a second json file that is more like an array, which has
[
{
"0500000US01001": 51289.0,
"0500000US01009": 46793.0,
"0500000US01017": 39857.0,
"0500000US01021": 48859.0
}
]
and so on, but basically it has the ID -> median home price per county. The ID's are the same between these 2 files, and of the same quantity. So I need get a 3rd json file out of these, which has "type", "arcs", "id", and "PRICE" (the addition).
These files are huge - any suggestions? I tried using jq but received an error that
jq: error ... object ({"type":"To...) and array ([{"0500000U...) cannot be multiplied
Thanks in advance!
A straightforward approach would be saving the second file into a variable and using it as a reference while updating the first file. E.g:
jq 'add as $prices | input
| .objects.us_counties_20m.geometries[] |= . + {PRICE: $prices[.id]}' file2 file1
add can be substituted with .[0] if the array in file2 contains only one object.
Online demo

GeoJson features coordinates are in meters not in [lng, lat]. How to display in google map?

Eg: [360590, 555610] - [lng, lat] in meters from google map api
- GeoJson data
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
360590,
555610
],
[
360590,
555555.0128
],
[
360590,
555540
],
[
360592.4439,
555540
],
[
360600,
555540
],
[
360600,
555518.8277
]
]
]
]
}
}
]
}
here, [360590, 555610] - [X, Y] coordinates is in meters, Now we need to display this coordinates on google map, Is there any solution for this?
also we must have to use addGeoJson or loadGeoJson method because we have 200MB data in GeoJson file.
Now we need to display this coordinates on google map, Is there have any solution for this?
Are you sure that these coordinates are in a meter?
It may be EPSG: 27700 or EPSG:4326 so you can try with it.
and you this link
QGIS
for convert coordinates and It might be useful for you.
Mapbox has a Utility class that can perform the conversion of meters to latitude/longitude for you:
public static Vector2d MetersToLatLon(Vector2d m)
Converts Spherical Mercator EPSG:900913 in xy meters to WGS84 lat/lon.
public static Vector2d LatLonToMeters(Vector2d v)
Converts Vector2d struct, WGS84 lat/lon to Spherical Mercator EPSG:900913 xy meters.
If you're looking to do the conversion yourself, then a simple approach is the following:
Assume the earth is a sphere with a circumference of 40,075km.
Length in meters of 1° of latitude is always 111.32km
Length in meters of 1° of longitude = 40,075 km * cos(latitude) / 360

PathSegList is deprecated and removed in Chrome 48

In Chrome 48, PathSegList is removed. And as I read in the answers to another question "Alternative for deprecated SVG pathSegList", Chrome is providing a new API, but I guess this new API is not yet available. What is another alternative and how can I use it. I know this is duplicate, but the link I mentioned is not helping me.
You do not need path seg polyfill (pathSeg.js).
With path data polyfill, you can edit path data as a common array object.
Use path data polyfill to work with new API. It's recommended.
var path = document.querySelector('path'); //your <path> element
//Be sure you have added the pathdata polyfill to your page before use getPathData
var pathdata = path.getPathData();
console.log(pathdata);
/*
you will get an Array object contains all path data details
like this:
[
{ "type": "M", "values": [ 50, 50 ] },
{ "type": "L", "values": [ 200, 200 ] }
]
*/
//replacement for createSVGPathSegMovetoRel and appendItem
pathdata.push({type:'m', values:[200,100]});
path.setPathData(pathdata);
//replacement for createSVGPathSegMovetoAbs and appendItem
pathdata.push({type:'M', values:[300,120]});
path.setPathData(pathdata);
//replacement for createSVGPathSegLinetoAbs and appendItem
pathdata.push({type:'L', values:[400,120]});
path.setPathData(pathdata);
console.log(path.getAttribute('d'));
//create a new path data array
var pathdata = [
{ "type": "M", "values": [ 50, 50 ] },
{ "type": "L", "values": [ 200, 200 ] }
];
path.setPathData(pathdata);
console.log(path.getAttribute('d'));