Convert GeoJSON object to GML geometry - jackson

I have the following issue with transforming GeoJSON object into GML geometry object.
First, on backend in request I retrieve GeoJSON object.
Then I use jackson library to transform it into java object. This java object is FeatureCollection from geojson-jackson library.
So I have java object representing GeoJSON on backend and now I need to transform it into GML object and extract geometry from it.
From this:
{
"featureCollection : {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [ [[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]] ]
},
}]
}
}
I need to get this:
<gml:Rectangle srsName="urn:x-ogc:def:crs:EPSG:6.6:25833">
<gml:exterior>
<gml:LinearRing>
<gml:coordinates>100.0,0.0 101.0,0.0 101.0,1.0 100.0,1.0 100.0,0.0</gml:coordinates>
</gml:LinearRing>
</gml:exterior>
</gml:Rectangle>
The question, is there any existing library which allows transform from GeoJSON into GML object?

Geotools can probably do what you need. I would not have posted, since I have not done this, but a speculative recipe is better than "definitely not".
GeoTools can read GeoJSON and give you a JTS geometry or a Feature, which can then be used to generate GML using a org.geotools.xml.Encoder.
Use org.geotools.geojson.feature.FeatureJSON or org.geotools.geojson.geom.GeometryJSON to take a JSON string and give a Geometry instance, like this:
GeometryJSON gJson = new GeometryJSON();
jtsGeometry = gJson.readGeometryCollection(jsonInputStream)
Then configure an encoder to write GML.
org.geotools.xml.Encoder encoder = new org.geotools.xml.Encoder(...);
...
encoder.encode(jtsGeometry, qName, outputStream);
I hope this is enough to started. Perhaps if you get it working (or find it impossible) come back and edit this post.

As far as i know, there is no such thing for java. ogr2ogr (a commandline utility) can do that:
converts simple features data between file formats
http://www.gdal.org/ogr2ogr.html
ogr2ogr -f "GML" destination.gml source.geojson
You can run that from java, see this question: How to run GDAL (ogr2ogr) in Java to convert Shapefiles to GeoJSON

No, there is definetly not. You have to find another way to do that.
I suggest you to do that either manually or read it as file and save to variables.

Related

Best Way to Iterate Through JSON Object Passed to Feature File in Karate

I have a JSON object like {"id1": 123, "id2": 234}, that I pass to a feature file in order to plugin each of the values above for a series of API requests like:
And path `/somePath/${id1}/detail`
And request {"id": "#(id1)", "action": "Reassign"}
My first thought was to use a Scenario Outline, but couldn't figure out how to set the ID's in the Examples table so they were properly read. Then I looked in documentation, and it seems like when I pass a JSON object like this, it should run the scenario for each value automatically. The problem is, not sure how to set the variable, as the key changes each time.
Or maybe there is a much better way to do this I'm not seeing?
In order for it to work your keys need to be the same. In this case you actually have a single JSON object with two properties (id1, id2). Instead what it sounds like you want are two objects:
[
{ "id": 123, "path": "/mydir1/" },
{ "id": 456, "path": "/mydir2/" }
]
Also notice that it has to be a list/array of json objects that you are passing in, not just one blob like you have. And if each object contains a path name, you should add that internal to the object itself.
But, again, notice that each object in the list/array has the same structure, just with different data. You then use the common property names in your script.

How to automatically unbox root array with moshi

The API I consume, thank to Retrofit, return a lot of json which the root level consist of a container with only a list inside of it, for example:
{
"data": [
...
]
}
In this example the name is "data" but it may vary depending on the route.
Is there a way to automatically unbox it so only the list is returned ?
Follow this article to implement this pattern:https://medium.com/#naturalwarren/moshi-made-simple-jsonqualifier-b99559c826ad

Generating JSON schema from syncthing JSON

Trying to generate JSON schema (http://jsonschema.net) from the syncthing (https://docs.syncthing.net/rest/system-connections-get.html) JSON below.
The problem is that the connection objects start with their ID (e.g.
YZJBJFX-RDB...) which is interpreted as a type.
Is it the JSON from synching that isn't standard or is it the issue with the schema generator?
Do you have any suggestions how to get around this if schema generation is a requirement (I.e. no typing schemas manually).
{
"total":{
"paused":false,
"clientVersion":"",
"at":"2015-11-07T17:29:47.691637262+01:00",
"connected":false,
"inBytesTotal":1479,
"type":"",
"outBytesTotal":1318,
"address":""
},
"connections":{
"YZJBJFX-RDBL7WY-6ZGKJ2D-4MJB4E7-ZATSDUY-LD6Y3L3-MLFUYWE-AEMXJAC":{
"connected":true,
"inBytesTotal":556,
"paused":false,
"at":"2015-11-07T17:29:47.691548971+01:00",
"clientVersion":"v0.12.1",
"address":"127.0.0.1:22002",
"type":"TCP (Client)",
"outBytesTotal":550
},
"DOVII4U-SQEEESM-VZ2CVTC-CJM4YN5-QNV7DCU-5U3ASRL-YVFG6TH-W5DV5AA":{
"outBytesTotal":0,
"type":"",
"address":"",
"at":"0001-01-01T00:00:00Z",
"clientVersion":"",
"paused":false,
"inBytesTotal":0,
"connected":false
},
"UYGDMA4-TPHOFO5-2VQYDCC-7CWX7XW-INZINQT-LE4B42N-4JUZTSM-IWCSXA4":{
"address":"",
"type":"",
"outBytesTotal":0,
"connected":false,
"inBytesTotal":0,
"paused":false,
"at":"0001-01-01T00:00:00Z",
"clientVersion":""
}
}
}
Any input is appreciated.
Is it the JSON from synching that isn't standard or is it the issue
with the schema generator?
There is nothing non-standard about this JSON. Neither is there any issue with the schema generation.
Unfortunately, defining a schema for what is effectively dynamic content is difficult. This will always be the case because the job of schemas is to describe static data structures.
That said, it may be possible to do this using the patternProperties field in JSON schema. This post is effectively asking the same question as yours.

Adding data points from non-cumulocity sensor

I created a measurements Java class called LoraRfInfo. I add LoraRfInfo as a measurement into Cumulocity as follows:
measurementRep.setSource(mo);
measurementRep.setType("tl_LoraRfInfo");
measurementRep.set(loraRfSignal);
measurementRep.setTime(new Date());
measurements.create(measurementRep);
I would like to visualize the stored LoraRfInfo measurement data in a "data points graph" widget, but can't figure out how to do that. Anyone knows how to visualize custom data points (i.e. non c8y_xxx) in a Cumulocity "data points graph" widget?
It's been a while since the question came up, but this might be useful for anyone who's looking for this.
In Java, you can create your own class if you want to visualize your custom data. The value however always has to be in a
com.cumulocity.model.measurement.MeasurementValue
Therefore, MeasurementValue has to be a property in your custom class.
The display name in Cumulocity will always be packageName_ClassName.
To use measurements in widgets, they need to follow the structure described in http://cumulocity.com/guides/reference/measurements/. On REST level, this structure is
{ "tl_LoraRfSignal": {
"<your measurement>": { "value": ..., "unit": "..." },
"<your measurement>": { "value": ..., "unit": "..." },
...
}
(Assuming that the class behind your variable loraRfSignal is tl.LoraRfSignal.)
On Java level, add properties of type MeasurementValue to locaRfSignal. An example can be found here: https://bitbucket.org/m2m/cumulocity-clients-java/src/53216dc587e24476e0578b788672416e8566f92b/device-capability-model/src/main/java/c8y/SignalStrength.java?at=default&fileviewer=file-view-default.
The "c8y_" in the beginning does not matter. It's merely a naming convention.

Element ':item' contains data from a type that maps to the name 'http://...:Location'. The deserializer has no knowledge of any type that maps to thi

I'm writing code to consume the Bing Geocode service (Bing Maps), and I am successfully able to pull JSON data via wcf, unfortunately I seem to be unable to map a piece of the data returned.
I created all the appropriate DataContracts as needed and populated them with the required members, but I when I start getting into the children I get the following error:
Element ':item' contains data from a type that maps to the name 'http://schemas.microsoft.com/search/local/ws/rest/v1:Location'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'Location' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.
So I commented out the "children" objects, and was basically able to discern that it was blowing up when it tries to read the 'Location' part of the JSON object
in my code below, it concerns the part here:
"__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
for what its worth, the url is bad, but I don't care. I don't want to use that type (which apparently maps back to a schema at Microsoft's website). Is there a way to tell WCF to ignore that link? Its not like I can.
What Bing returns
{
"authenticationResultCode":"ValidCredentials",
"brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
"copyright":"Copyright © 2010 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
"resourceSets":[
{
"estimatedTotal":1,
"resources":[
{
"__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
"bbox":[
47.635884282429323,
-122.13737419709076,
47.643609717570676,
-122.12208780290925
],
"name":"1 Microsoft Way, Redmond, WA 98052-8300",
"point":{
"type":"Point",
"coordinates":[
47.639747,
-122.129731
]
},
"address":{
"addressLine":"1 Microsoft Way",
"adminDistrict":"WA",
"adminDistrict2":"King County",
"countryRegion":"United States",
"formattedAddress":"1 Microsoft Way, Redmond, WA 98052-8300",
"locality":"Redmond",
"postalCode":"98052-8300"
},
"confidence":"High",
"entityType":"Address"
}
]
}
],
"statusCode":200,
"statusDescription":"OK",
"traceId":"43c6a4dc130749bbb14eb72bf12c4198 "
}
Found it. Its because I had to accomodate for the ref'd __type in my data contract (it needed to know what type to use. The solution was this:
[DataContract(Namespace = "http://schemas.microsoft.com/search/local/ws/rest/v1", Name="Location")]
btw, I found the answer here
: Problem with deserializing JSON on datamember "__type"