use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class GeoJSONReadTest method mixed_arrays.
@Test
public void mixed_arrays() throws Exception {
try (final InputStream resource = GeoJSONReadTest.class.getResourceAsStream("/org/apache/sis/internal/storage/geojson/mixed_arrays.json")) {
if (resource == null)
throw new IllegalStateException("Cannot find a test resource");
final GeoJSONObject readValue = GeoJSONParser.parse(resource);
assertNotNull(readValue);
assertTrue(readValue instanceof GeoJSONFeature);
final GeoJSONFeature feature = (GeoJSONFeature) readValue;
// Ensure no side-effect would break other parts of the feature reading
final GeoJSONGeometry geom = feature.getGeometry();
assertTrue("Read feature should contain a point, but we've read: " + geom, geom instanceof GeoJSONGeometry.GeoJSONPoint);
// Now, we can check our arrays have been well-parsed
final Map<String, Object> properties = feature.getProperties();
assertPropertyIs(new double[] { 2., 3., 4. }, "numberMix1", properties);
assertPropertyIs(new double[] { 2., 3., 4. }, "numberMix2", properties);
assertPropertyIs(new int[] { 42, 51 }, "intArray", properties);
assertPropertyIs(new long[] { 1, 7_000_000_000l }, "longArray", properties);
assertPropertyIs(new Double[] { 2., null, 4. }, "numbersWithNullValues", properties);
assertPropertyIs(new Object[] { null, null }, "onlyNullValues", properties);
assertPropertyIs(new Object[0], "emptyArray", properties);
assertPropertyIs(new Object[] { 2.0, "I'm a text", null }, "arbitraryMix", properties);
}
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class GeoJSONReadTest method parsingOrderBugTest.
@Test
public void parsingOrderBugTest() throws IOException {
String geoJsonPolygonString = "" + " {\n" + " \"coordinates\": [\n" + " [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],\n" + " [100.0, 1.0], [100.0, 0.0] ]\n" + " ],\n" + " \"type\": \"Polygon\"\n" + " }";
GeoJSONObject obj = GeoJSONParser.parse(new ByteArrayInputStream(geoJsonPolygonString.getBytes()));
Assert.assertTrue("A geometry should have been decoded", obj instanceof GeoJSONGeometry.GeoJSONPolygon);
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class ConvertersTestUtils method getGeometryArrayFromInputStream.
/**
* Helper method that loads a geometry array from a json file
* @param inputStream inputStream on the json file
* @return a geometry array
* @throws IOException
* @throws FactoryException
*/
public static Geometry[] getGeometryArrayFromInputStream(final InputStream inputStream) throws IOException, FactoryException {
final GeoJSONObject geoJsonObject = GeoJSONParser.parse(inputStream);
if (!(geoJsonObject instanceof GeoJSONGeometry.GeoJSONGeometryCollection))
fail();
final GeoJSONGeometry.GeoJSONGeometryCollection geometryCollection = (GeoJSONGeometry.GeoJSONGeometryCollection) geoJsonObject;
final Geometry parentGeometry = WPSConvertersUtils.convertGeoJSONGeometryToGeometry(geometryCollection);
final Geometry[] geometryArray = new Geometry[parentGeometry.getNumGeometries()];
for (int i = 0; i < geometryArray.length; i++) geometryArray[i] = parentGeometry.getGeometryN(i);
return geometryArray;
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class WPSConvertersUtils method readGMLGeometryFromString.
/**
* Helper method which extracts the GML Geometry from a String.
*
* @param content content string containing a GML geometry
* @return a GeoJSONObject
* @throws java.io.IOException on reading errors
*/
public static AbstractGeometry readGMLGeometryFromString(String content) throws IOException {
ArgumentChecks.ensureNonEmpty("content", content);
// Parse GeoJSON
try (final StringReader contentReader = new StringReader(content)) {
Unmarshaller um = GMLMarshallerPool.getInstance().acquireUnmarshaller();
Object obj = um.unmarshal(contentReader);
GMLMarshallerPool.getInstance().recycle(um);
if (obj instanceof JAXBElement) {
obj = ((JAXBElement) obj).getValue();
}
if (!(obj instanceof AbstractGeometry)) {
throw new IOException("XML don't contains GML geometry");
}
return (AbstractGeometry) obj;
} catch (JAXBException ex) {
throw new IOException(ex);
}
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class ComplexToGeometryArrayConverter method convert.
/**
* {@inheritDoc}
* @return Geometry array.
*/
@Override
public Geometry[] convert(final Data source, final Map<String, Object> params) throws UnconvertibleObjectException {
String dataMimeTypeIdentifier = null;
try {
final List<Object> data = source.getContent();
if (!data.isEmpty()) {
if (WPSMimeType.APP_GML.val().equalsIgnoreCase(source.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(source.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(source.getMimeType())) {
dataMimeTypeIdentifier = "GML";
final List<Geometry> geoms = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
Object val = data.get(i);
if (val instanceof JAXBElement) {
val = ((JAXBElement) val).getValue();
}
if (val instanceof AbstractGeometry) {
geoms.add(GeometrytoJTS.toJTS((AbstractGeometry) val));
} else {
throw new UnconvertibleObjectException("Unknown geometry type at index: " + i);
}
}
return geoms.toArray(new Geometry[geoms.size()]);
} else // array is passed as a GeoJSON FeatureCollection
if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(source.getMimeType())) {
dataMimeTypeIdentifier = "GeoJSON";
final String content = WPSConvertersUtils.geojsonContentAsString(source);
final GeoJSONObject jsonObject = WPSConvertersUtils.readGeoJSONObjectsFromString(content);
// Check that we have a FeatureCollection
if (!(jsonObject instanceof GeoJSONGeometryCollection))
throw new UnconvertibleObjectException("Expected a feature collection. Found : " + jsonObject.getClass().getName());
// Check collection size, if it's empty raise an exception
final GeoJSONGeometryCollection geometryCollection = (GeoJSONGeometryCollection) jsonObject;
int collectionSize = geometryCollection.getGeometries().size();
if (collectionSize == 0)
throw new UnconvertibleObjectException("Empty feature collection.");
// Fill the Geometry array
final Geometry[] geometryArray = new Geometry[collectionSize];
Iterator<GeoJSONGeometry> iterator = geometryCollection.getGeometries().iterator();
int index = 0;
while (iterator.hasNext()) {
final GeoJSONGeometry jsonGeometry = iterator.next();
// GeometryUtils.toJTS(jsonGeometry, crs);
geometryArray[index] = WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry);
index++;
}
return geometryArray;
} else
throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + source.getMimeType());
} else {
throw new UnconvertibleObjectException("Invalid data input : Empty geometry list.");
}
} catch (FactoryException ex) {
throw new UnconvertibleObjectException("Invalid data input : Cannot convert " + dataMimeTypeIdentifier + " geometry.", ex);
} catch (MalformedURLException ex) {
throw new UnconvertibleObjectException("Unknown CRS code or unable to read the CRS from a geometry", ex);
} catch (IOException ex) {
throw new UnconvertibleObjectException(ex);
}
}
Aggregations