Related
TL;DR
How can I return the whole JSON after filtering inner array elements of a top-level key?
Detailed explanation
I have a JSON describing the COCO image database and it is formatted as follows (irrelevant elements truncated as ...).
{
"info": {
"description": "COCO 2017 Dataset",
...
},
"licenses": [
{
"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
...
},
...
],
"images": [
{
"license": 4,
...
},
"annotations": [
{
"segmentation": [
[
510.66,
...
]
],
"area": 702.1057499999998,
"iscrowd": 0,
"image_id": 289343,
"bbox": [
473.07,
395.93,
38.65,
28.67
],
"category_id": 18,
"id": 1768
},
"categories": [
{
"supercategory": "person",
...
},
]
}
I need to filter annotations where category_id has one of several values, for example 1, 2.
I can successfully filter such category_ids with
jq -C ' .annotations[] | select( .category_id == 1 or .category_id == 2 ) ' instances_val2017.json | less -R
However, what is returned are only the annotations element of the total JSON as below.
{
"segmentation": [
[
162.72,
...
]
],
"area": 426.9120499999995,
"iscrowd": 0,
"image_id": 45596,
"bbox": [
161.52,
507.18,
46.45,
19.16
],
"category_id": 2,
"id": 124742
}
{
...
{
I know it's possible to return these elements as an array by wrapping the expression in [] but how can I return the entire original JSON after filtering the specified category ids?
Okay I spent 3 hours trying to solve this yesterday then this morning I posted this question and subsequently figured it out!
Here is the solution which uses the |= operator which modifies an element in place.
jq '.annotations |= map(select(.category_id | contains(1,2)))' instances_val2017.json
As per the suggestion of #peak, here is the command with == instead of contains.
jq '.annotations |= map(select(.category_id == (1,2)))' instances_val2017.json
I am having a JSON object like below:
{
"ItemIsExportControlled": [
"true",
"false"
],
"OrderShippingDestination": [
"domestic",
"foreign"
],
"CustomerCreditStatus": [
"approved",
"denied",
"unknown"
],
"OrderShipping": [
"ground",
"air",
"sea"
],
"ItemStockStatus": [
"validInStock",
"invalid",
"validOutOfStock"
],
"OrderDeliveryTimeframe": [
"oneMonth",
"immediate",
"oneWeek"
],
"OrderPricingScheme": [
"scheme2",
"scheme1",
"scheme3"
]
}
I am getting two values from onChange event in variables say item and value, so what I need to do is to compare item with this JSON structure keys, and if it matches, then I want to replace the values of that corresponding key with the new value that I got in value variable.
For e.g. If item is ItemIsExportControlled and value is false. Then I want my resulting JSON structure to be
{
"ItemIsExportControlled": [
"false"
],
"OrderShippingDestination": [
"domestic",
"foreign"
],
"CustomerCreditStatus": [
"approved",
"denied",
"unknown"
],
"OrderShipping": [
"ground",
"air",
"sea"
],
"ItemStockStatus": [
"validInStock",
"invalid",
"validOutOfStock"
],
"OrderDeliveryTimeframe": [
"oneMonth",
"immediate",
"oneWeek"
],
"OrderPricingScheme": [
"scheme2",
"scheme1",
"scheme3"
]
}
Can anyone please help me on this. Thanks in advance..
Its simple lets say your JSON is :
const YOUR_JSON = {
"ItemIsExportControlled": [
"true",
"false"
],
"OrderDeliveryTimeframe": [
"oneMonth",
"immediate",
"oneWeek"
],
"OrderPricingScheme": [
"scheme2",
"scheme1",
"scheme3"
]
}
And this could be your onChange function where you are getting item and value
onChange (item, value) {
const result = {...YOUR_JSON, [item]: [value]};
}
You will have updated JSON in result
I've json like below, which i got from below URL:
{
"info" : {
"1484121600" : [
212953175.053333,212953175.053333,null
],
"1484125200" : [
236203014.133333,236203014.133333,236203014.133333
],
"1484128800" : [
211414832.968889,null,211414832.968889
],
"1484132400" : [
208604573.791111,208604573.791111,208604573.791111
],
"1484136000" : [
231358374.288889,231358374.288889,231358374.288889
],
"1484139600" : [
210529301.097778,210529301.097778,210529301.097778
],
"1484143200" : [
212009682.04,null,212009682.04
],
"1484146800" : [
232364759.566667,232364759.566667,232364759.566667
],
"1484150400" : [
218138788.524444,218138788.524444,218138788.524444
],
"1484154000" : [
218883301.282222,218883301.282222,null
],
"1484157600" : [
237874583.771111,237874583.771111,237874583.771111
],
"1484161200" : [
216227081.924444,null,216227081.924444
],
"1484164800" : [
227102054.082222,227102054.082222,null
]
},
"summary" : "data",
"end" : 1484164800,
"start": 1484121600
}
I'm fetching this json from some url using jsonlite package in R like below:
library(jsonlite)
input_data <- fromJSON(url)
timeseries <- input_data[['info']] # till here code is fine
abc <- data.frame(ds = names(timeseries[[1]]),
y = unlist(timeseries[[1]]), stringsAsFactors = FALSE)
(something is wrong in above line)
I need to convert this data in timeseries variable into data frame; which will have index column as the epoch time and no. of columns in dataframe will depend upon no. of values in array and all arrays will have same no. of values for sure. But no. of values in array can be 1 0r 2 or etc; it is not fixed. Like in below example array size is 3 for all.
for eg : dataframe should look like:
index y1 y2 y3
1484121600 212953175.053333 212953175.053333 null
1484125200 236203014.133333 236203014.133333 236203014.133333
Please suggest how do I do this in R. I'm new to it.
JSON with only 1 item in array:
{
"info": {
"1484121600": [
212953175.053333
],
"1484125200": [
236203014.133333
],
"1484128800": [
211414832.968889
],
"1484132400": [
208604573.791111
],
"1484136000": [
231358374.288889
],
"1484139600": [
210529301.097778
],
"1484143200": [
212009682.04
],
"1484146800": [
232364759.566667
],
"1484150400": [
218138788.524444
],
"1484154000": [
218883301.282222
],
"1484157600": [
237874583.771111
],
"1484161200": [
216227081.924444
],
"1484164800": [
227102054.082222
]
},
"summary": "data",
"end": 1484164800,
"start": 1484121600
}
Consider binding the list of json values to a matrix with sapply(), then transpose columns to rows with t(), and finally convert to dataframe with data.frame()
abc <- data.frame(t(sapply(timeseries, c)))
colnames(abc) <- gsub("X", "y", colnames(abc))
abc
# y1 y2 y3
# 1484121600 212953175 212953175 NA
# 1484125200 236203014 236203014 236203014
# 1484128800 211414833 NA 211414833
# 1484132400 208604574 208604574 208604574
# 1484136000 231358374 231358374 231358374
# 1484139600 210529301 210529301 210529301
# 1484143200 212009682 NA 212009682
# 1484146800 232364760 232364760 232364760
# 1484150400 218138789 218138789 218138789
# 1484154000 218883301 218883301 NA
# 1484157600 237874584 237874584 237874584
# 1484161200 216227082 NA 216227082
# 1484164800 227102054 227102054 NA
What I'm trying to do is to find places which are inside or with 10 meters distance of a street.
My streets table has a geometry column which coordinates are saved from.
A sample coordinate looks like
{ "type": "Feature", "properties": { "id": 4.000000, "osm_id": 69551269.000000, "type": "tertiary", "name": "Street name", "tunnel": 0, "bridge": 0, "oneway": 1, "ref": null, "z_order": 4.000000, "access": null, "service": null, "class": "highway" },
"geometry": { "type": "LineString",
"coordinates": [ [ 45.055701068545773, 37.537045660463036 ], [ 45.055566036085651, 37.536995369044007 ], [ 45.054243455583901, 37.536797891405229 ], [ 45.053941120336447, 37.536756233346466 ], [ 45.053692177812167, 37.536712228354787 ], [ 45.052483758831642, 37.536435290273943 ], [ 45.052157870436275, 37.536344765719662 ], [ 45.051875819394468, 37.536229430731993 ], [ 45.05173206975504, 37.536151395213466 ], [ 45.051607347035826, 37.536067827638817 ], [ 45.051492766419436, 37.535979063284202 ], [ 45.050636052096081, 37.535314881276747 ], [ 45.050383253896371, 37.535111536305749 ], [ 45.050164989137727, 37.534882458892014 ], [ 45.050017048546714, 37.534692692604175 ], [ 45.049976061040212, 37.534639970433204 ], [ 45.049796436855189, 37.534394380670221 ], [ 45.049439032503869, 37.533859196152598 ], [ 45.049149186292141, 37.533424929749174 ], [ 45.048739143588875, 37.532811039160741 ], [ 45.048373357334377, 37.532213577102539 ], [ 45.048231284075598, 37.531903279047071 ], [ 45.048143022635173, 37.531710579093094 ], [ 45.047949568309946, 37.531336494754463 ], [ 45.047873628267183, 37.531189895267971 ], [ 45.047984772303266, 37.53111303321586 ] ] } },
My question is that should the coordinates be saved as polygon or LineString.
GeoJson authoritive indicates that LineString has no inside or outside but the data which I've got from openstreet(above is a sample) has a type of LineString.
So Which one should I use?
Edit:
A sample LineString from openstreetmap looks like this:
I drew them by drawing a polygon with coordinates.
Either of the two of geometry types, linestrings and polygon would work for you in your case. But i think it is quite straightforward to convert the linestrings to polygon and just use ST_Buffer the polygon with 20m...
SET #line = ST_GeomFromText('LINESTRING(44.9894318 37.496227, 44.9901579 37.4964403)',4326); SET #pt = ST_GeomFromText('POINT(45.00 37.4964)',4326); SET #buffer=st_buffer(#line,0.0124274); SELECT ST_WITHIN(#pt,#buffer)
20 meters = 0.0124274 mile,
Difference between Geography coordinate system and geometry coordinate system: https://en.wikipedia.org/wiki/Geographic_coordinate_system
I have multiple JSON files (poly1.json, poly2.json) with the following setup:
{
"Polygon1": {
"name": "poly1",
"specifications": [
{
"areaGeometry": {
"type": "Polygon",
"coordinates": [
[
[
5.129820025,
52.085407733
],
[
5.129117875,
52.086181679
],
[
5.128497179,
52.087946286
],
[
5.128458022,
52.088253322
],
[
5.12866837,
52.088507157
],
[
5.129251266,
52.088976802
],
[
5.129473861,
52.08926905
],
[
5.129385309,
52.089499203
],
[
5.12909759,
52.089698198
],
[
5.127961124,
52.090148712
],
[
5.127685173,
52.090462912
],
[
5.127310682,
52.091653473
],
[
5.12710699,
52.092271708
],
[
5.127126612,
52.092518366
],
[
5.128237531,
52.093468305
],
[
5.128130926,
52.093688728
],
[
5.126525853,
52.094399058
],
[
5.126377274,
52.09459342
],
[
5.126284571,
52.095035437
],
[
5.130996578,
52.095312264
],
[
5.137138625,
52.095591962
],
[
5.139036247,
52.095628598
],
[
5.138962372,
52.095484813
],
[
5.137879856,
52.093651573
],
[
5.137480747,
52.093048367
],
[
5.136997815,
52.092468872
],
[
5.13643473,
52.091917507
],
[
5.135795776,
52.091398471
],
[
5.134288171,
52.090401311
],
[
5.133608279,
52.089984575
],
[
5.133259679,
52.089768435
],
[
5.132932239,
52.089549796
],
[
5.132656508,
52.089342179
],
[
5.132411195,
52.089120552
],
[
5.132198186,
52.088886599
],
[
5.132143714,
52.088818019
],
[
5.130950838,
52.087097103
],
[
5.130737143,
52.086736442
],
[
5.130575274,
52.086365674
],
[
5.130403794,
52.085570404
],
[
5.129783706,
52.08522338
],
[
5.129820025,
52.085407733
]
]
]
}
}
],
}
}
Now I want to draw all the polygons on Google Maps on where the user is looking at -if there is a polygon in his screen. What is the best way to do so? Should I use for each or are there better ways? How do you iterate over it? Would that not make it slow?
According to my understanding, there are two ways you can go about it. First, you can make a KML layer. You can put together all these polygons in one KML file and make an overlay and integrate that overlay with your map object. (Here's the JQuery reference)
Another way to do it is by using your JSON files and making a data layer. I would suggest to use the drag and drop GeoJSON code example as a reference. Because that will give you accuracy to the specific points. But you can always go for the simpler implementation, you can find other sample codes from Google on the left side of this link.