Not able to Parse a Json File - javascript

I want to load a GeoJson File that is uploaded on server
var promise = ('https://api.myjson.com/bins/31e3j');
that.map.data.loadGeoJson(promise);
This condition works fine
But I want to Load this GeoJson File locally
so I have assigned the Json Code instead a the Server Link to a Variable on which I am neither getting any error but unable to get the O/P as well
var promise = jQuery.parseJSON ('{ "type": "FeatureCollection","crs":{"type": "name","properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84"}},"features": [{"type": "Feature", "properties": {"id": 1},"geometry": {"type": "Polygon", "coordinates": [ [ [ -83.52936044652942, 40.30230752849768], [ -83.52924865349425, 40.30230753872012], [ -83.52924666169983, 40.3021800251207 ], [ -83.52935848418728, 40.302181900418084 ], [ -83.52936044652942, 40.30230752849768]]]}}, ]}');
that.map.data.loadGeoJson(promise);

When in doubt, run it through a linter/formatter:
http://jsonlint.com/
You have an error in the JSON, a comma a few characters from the end:
]]]}}, ]}');
^-------TROUBLE MAKER!
Or this one is cool!
http://pro.jsonlint.com/
I am neither getting any error
Maybe the surrounding code is swallowing the error. If you take your var promise = jQuery.parseJSON('DODGY_JSON_HERE') code and run it in the console, you'll see the error:
Uncaught SyntaxError: Unexpected token ](…)
e.extend.parseJSON #jquery.min.js:2
(anonymous function) #VM270:2
InjectedScript._evaluateOn #VM268:875
InjectedScript._evaluateAndWrap #VM268:808
InjectedScript.evaluate #VM268:664
Not as handy as the linter, but at least you see an error.

Invalid JSON is not parseable, obviously:
...snip...[ -83.52936044652942, 40.30230752849768]]]}}, ]}');
^----

Because that isn't correct JSON. You have additional comma at the end.
{ "type": "FeatureCollection","crs":{"type": "name","properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84"}},"features": [{"type": "Feature", "properties": {"id": 1},"geometry": {"type": "Polygon", "coordinates": [ [ [ -83.52936044652942, 40.30230752849768], [ -83.52924865349425, 40.30230753872012], [ -83.52924666169983, 40.3021800251207 ], [ -83.52935848418728, 40.302181900418084 ], [ -83.52936044652942, 40.30230752849768]]]}} ]}
This is correct JSON:
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"id": 1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-83.52936044652942,
40.30230752849768
],
[
-83.52924865349425,
40.30230753872012
],
[
-83.52924666169983,
40.3021800251207
],
[
-83.52935848418728,
40.302181900418084
],
[
-83.52936044652942,
40.30230752849768
]
]
]
}
}
]
}

Related

jsonschema-master to validate json request not responding as expected

I am trying to use jsonschema-master to validate a json request entered via a POST request using express. See the code and sample below.
It picks up if the attribute labels are missing or spelt wrong, such as “model”, “areas”, “id” but isn’t picking up if the values of those attributes meet the specifications. For example the “model” attribute is defined as an enumerated type either “premium” or “basic”, but I seem to be able to put any old string in there and it plows on regardless, also the coordinates are defined as type number, but again it ignores this and the error then gets passed the validator and causes problems further on. Not sure what I'm missing.
node.js code:
var Validator = require('jsonschema-master').Validator;
var v = new Validator();
var bodySchema = {
"model": {
"enum": [ "premium","basic" ]
},
"areas": {
"type":"array",
"items": {
"id": {"type": "string"},
"geometry": {
"type": { "type":"string"},
"coordinates": {
"type":"array",
"items": {
"type":"array",
"items": [
{"type":"number"},
{"type":"number"},
{"type":"number"}
]
}
},
"required" : ["type","coordinates"]
},
"required" : ["id","geometry"]
}
},
"required" : ["model","areas"]
};
var valResult = v.validate(doc.request, bodySchema);
if (valResult.errors.length) {
// Validation failed.
// All processing will now stop.
console.log('Request invalid: '+ doc._id +" - "+valResult.errors);
}
SAMPLE CORRECT JSON request (in doc.request)
{
"model": "premium",
"areas": [
{
"id": "1234",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
453600.0,
181100.0,
0
],
[
453600.0,
181200.0,
0
],
[
453700.0,
181200.0,
0
],
[
453700.0,
181100.0,
0
],
[
453600.0,
181100.0,
0
]
]
]
}
}
]
}
Thanks all, but I worked it out in the end. Here is the corrected syntax for the schema:
var bodySchema = {
"type" : "object",
"properties": {
"model": {
"enum": [ "premium","basic" ]
},
"areas": {
"type":"array",
"items": {
"type" : "object",
"properties": {
"id": {"type": "string"},
"geometry": {
"type" : "object",
"properties": {
"type": { "type":"string"},
"coordinates": {
"type":"array",
"items": {
"type":"array",
"items": {
"type":"array",
"items": [
{"type":"number"},
{"type":"number"},
{"type":"number"}
]
}
}
}
},
"required" : ["type","coordinates"]
}
},
"required" : ["id","geometry"]
}
}
},
"required" : ["model","areas"]
};

*pointsWithinPolygon* on Trufjs

I'm trying to get some point inside multiple polygon using pointsWithinPolygon in turfjs, but the result is unexpected.
Is there any chance that pointsWithinPolygon isn't compatible with FeatureCollection?
Here is the example of my usage.
let points = {
"type": "Point",
"coordinates": [
106.866995, -6.261513
]
}
let filter = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"IdArea": 4
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
106.314674,
-6.6348689124
],
[
107.5781628906,
-6.6348689124
],
[
107.5781628906,
-5.9742195859
],
[
106.314674,
-5.9742195859
],
[
106.314674,
-6.6348689124
]
]
]
}
}
]
}
let result = turf.pointsWithinPolygon(points, filter);
console.log(result);
You are trying to use geoJsons as inputs to turf's pointsWithinPolygons function.
The function takes a turf.points array of points, and a turf.polygon array of polygon vertices.
Based on the variables you established, you would need to call:
let result = turf.pointsWithinPolygon(turf.points([points.coordinates]), turf.polygon(filter.features.geometry.coordinates));

Split JSON File Objects Into Multiple Files

I have a file with too many data objects in JSON of the following form:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-37.880859375,
78.81903553711727
],
[
-42.01171875,
78.31385955743478
],
[
-37.6171875,
78.06198918665974
],
[
-37.880859375,
78.81903553711727
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-37.6171875,
78.07107600956168
],
[
-35.48583984375,
78.42019327591201
],
[
-37.880859375,
78.81903553711727
],
[
-37.6171875,
78.07107600956168
]
]
]
}
}
]
}
I would like to split the large file such that each features object would have its own file containing a its type object and features(coordinates) object. So essentially, I am trying to get many of these:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-37.6171875,
78.07107600956168
],
[
-35.48583984375,
78.42019327591201
],
[
-37.880859375,
78.81903553711727
],
[
-37.6171875,
78.07107600956168
]
]
]
}
}
]
}
Here's a solution requiring just one invocation of jq and one of awk, assuming the input is in a file (input.json) and that the N-th component should be written to a file /tmp/file$N.json beginning with N=1:
jq -c '.features = (.features[] | [.]) ' input.json |
awk '{ print > "/tmp/file" NR ".json"}'
An alternative to awk here would be split -l 1.
If you want each of the output files to be "pretty-printed", then using a shell such as bash, you could (at the cost of n additional calls to jq) write:
N=0
jq -c '.features = (.features[] | [.])' input.json |
while read -r json ; do
N=$((N+1))
jq . <<< "$json" > "/tmp/file${N}.json"
done
Each of the additional calls to jq will be fast, so this may be acceptable.
PowerShell solution (requires PowerShell v3 or newer):
$i = 0
Get-Content 'C:\path\to\input.json' -Raw |
ConvertFrom-Json |
Select-Object -Expand features |
ForEach-Object {
$filename = 'C:\path\to\feature{0:d5}.json' -f ($i++)
$properties = [ordered]#{
type = 'FeatureCollection'
features = $_
}
New-Object -Type PSObject -Property $properties |
ConvertTo-Json -Depth 10 |
Set-Content $filename
}
I haven't tested this code properly. But should provide you some idea on how you can solve the problem mentioned above
var json = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-37.880859375,
78.81903553711727
],
[
-42.01171875,
78.31385955743478
],
[
-37.6171875,
78.06198918665974
],
[
-37.880859375,
78.81903553711727
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-37.6171875,
78.07107600956168
],
[
-35.48583984375,
78.42019327591201
],
[
-37.880859375,
78.81903553711727
],
[
-37.6171875,
78.07107600956168
]
]
]
}
}
]
}
$(document).ready(function(){
var counter = 1;
json.features.forEach(function(feature){
var data = {type: json.type, features: [feature]}
var newJson = JSON.stringify(data);
var blob = new Blob([newJson], {type: "application/json"});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.download = "feature_" + counter + ".json";
a.href = url;
a.textContent = "Download feature_" + counter + ".json";
counter++;
document.getElementById('feature').appendChild(a);
document.getElementById('feature').appendChild(document.createElement('br'));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="feature"></div>

Create FeatureCollection from multiple GeoJSON strings

I have a list of features and I want to create a FeatureCollection to group them in one string, the features are from different types (polygons, lines and points).
Here is how they are written :
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
-31.640625,
15.623036831528264
],
[
-2.8125,
-14.264383087562637
],
[
-22.5,
-30.751277776257812
],
[
-30.937499999999996,
-38.54816542304657
]
]
}
}
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-24.960937499999996,
29.84064389983441
],
[
-2.109375,
21.616579336740603
],
[
2.4609375,
43.068887774169625
],
[
-31.289062500000004,
49.38237278700955
],
[
-24.960937499999996,
29.84064389983441
]
]
]
}
}
Is there a javascript function that deals with this, i'm stuck on it.
If you have an array of GeoJSON feature strings, you can create a GeoJSON feature collection string like this:
var features = [
'{ "type": "Feature", "properties": {}, ... }',
'{ "type": "Feature", "properties": {}, ... }'
];
var collection = JSON.stringify({
features: features.map(JSON.parse),
type: 'FeatureCollection'
});

Syntax error on JS script

I have the following .js file, and Dreamweaver reports a syntax error on line 2.
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 0,
"properties": {
"Id": 0,
"Title": "River & Trail HQ",
"Other": null,
"Parking": "Yes"
},
"geometry": {
"type": "Point",
"coordinates": [
-8649997.6690437607,
4769179.73322534
]
}
}
]
}
Seems something is wrong with
"type": "FeatureCollection",
line.
If that's the whole JavaScript file, then it's giving you an error because it's interpreted as a block instead of an object literal. To fix this, you could assign it to something, like
var someObj = {
"type": "FeatureCollection",
...
}
You may also be thinking of a JSON file (which is probably the case, since a free-floating object literal isn't going to do you much good anyway). JSON and JavaScript are not the same. If you really want a JSON file, then save it as such (.json).
Try
var x = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature", "id": 0, "properties":
{
"Id": 0, "Title": "River & Trail HQ", "Other": null, "Parking": "Yes"
},
"geometry":
{
"type": "Point", "coordinates": [ -8649997.6690437607, 4769179.73322534 ]
}
}
]};
http://jsonlint.com shows it as valid JSON and I can't see any issues with it on visual inspection.

Categories

Resources