use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class GeoJSONStore method readType.
/**
* Read FeatureType from a JSON-Schema file if exist or directly from the
* input JSON file.
*
* @return
* @throws DataStoreException
* @throws IOException
*/
private FeatureType readType() throws DataStoreException, IOException {
if (Files.exists(descFile) && Files.size(descFile) != 0) {
// build FeatureType from description JSON.
return FeatureTypeUtils.readFeatureType(descFile);
} else {
if (Files.exists(jsonFile) && Files.size(jsonFile) != 0) {
final String name = GeoJSONUtils.getNameWithoutExt(jsonFile);
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName(name);
// build FeatureType from the first Feature of JSON file.
final GeoJSONObject obj = GeoJSONParser.parse(jsonFile, true);
if (obj == null) {
throw new DataStoreException("Invalid GeoJSON file " + jsonFile.toString());
}
CoordinateReferenceSystem crs = GeoJSONUtils.getCRS(obj);
if (obj instanceof GeoJSONFeatureCollection) {
GeoJSONFeatureCollection jsonFeatureCollection = (GeoJSONFeatureCollection) obj;
if (!jsonFeatureCollection.hasNext()) {
// empty FeatureCollection error ?
throw new DataStoreException("Empty GeoJSON FeatureCollection " + jsonFile.toString());
} else {
// TODO should we analyse all Features from FeatureCollection to be sure
// that each Feature properties JSON object define exactly the same properties
// with the same bindings ?
GeoJSONFeature jsonFeature = jsonFeatureCollection.next();
fillTypeFromFeature(ftb, crs, jsonFeature, false);
}
} else if (obj instanceof GeoJSONFeature) {
GeoJSONFeature jsonFeature = (GeoJSONFeature) obj;
fillTypeFromFeature(ftb, crs, jsonFeature, true);
} else if (obj instanceof GeoJSONGeometry) {
ftb.addAttribute(String.class).setName("fid").addRole(AttributeRole.IDENTIFIER_COMPONENT);
ftb.addAttribute(findBinding((GeoJSONGeometry) obj)).setName("geometry").setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
}
return ftb.build();
} else {
throw new DataStoreException("Can't create FeatureType from empty/not found Json file " + jsonFile.getFileName().toString());
}
}
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class GeoJSONStore method getEnvelope.
@Override
public Optional<Envelope> getEnvelope() throws DataStoreException {
Envelope envelope = null;
rwLock.readLock().lock();
try {
final GeoJSONObject obj = GeoJSONParser.parse(jsonFile, true);
final CoordinateReferenceSystem crs = GeoJSONUtils.getCRS(obj);
envelope = GeoJSONUtils.getEnvelope(obj, crs);
} catch (IOException e) {
throw new DataStoreException(e.getMessage(), e);
} finally {
rwLock.readLock().unlock();
}
return Optional.ofNullable(envelope);
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class ConvertersTestUtils method getGeometryFromGeoJsonContent.
/**
* Helper method that read a geometry in a json file
* @param path path of the file containing the geometry
* @return the read geometry
* @throws IOException on IO errors when reading the file
* @throws FactoryException when the crs of the geometry cannot be retrieved
*/
public static Geometry getGeometryFromGeoJsonContent(final Path path) throws IOException, FactoryException {
final GeoJSONObject geoJsonObject = GeoJSONParser.parse(path);
GeoJSONGeometry geoJsonGeometry = null;
if (geoJsonObject instanceof GeoJSONFeature) {
GeoJSONFeature jsonFeature = (GeoJSONFeature) geoJsonObject;
geoJsonGeometry = jsonFeature.getGeometry();
} else if (geoJsonObject instanceof GeoJSONGeometry)
geoJsonGeometry = (GeoJSONGeometry) geoJsonObject;
else
fail();
return WPSConvertersUtils.convertGeoJSONGeometryToGeometry(geoJsonGeometry);
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class GeometryToComplexConverterTest method getGeometry.
/**
* Helper method that read a json file containing a single Geometry
* @param jsonFile file to read
* @return a geometry
* @throws IOException
* @throws FactoryException
*/
private static Geometry getGeometry(Path jsonFile) throws IOException, FactoryException {
final GeoJSONObject geoJsonObject = GeoJSONParser.parse(jsonFile);
if (!(geoJsonObject instanceof GeoJSONGeometry))
fail();
final GeoJSONGeometry jsonGeometry = (GeoJSONGeometry) geoJsonObject;
return WPSConvertersUtils.convertGeoJSONGeometryToGeometry(jsonGeometry);
}
use of org.geotoolkit.internal.geojson.binding.GeoJSONObject in project geotoolkit by Geomatys.
the class GeoJSONReadTest method parserTest.
/**
* Test GeoJSONParser full and lazy reading on FeatureCollection
*/
@Test
public void parserTest() throws URISyntaxException, IOException {
URL fcFile = GeoJSONReadTest.class.getResource("/org/apache/sis/internal/storage/geojson/featurecollection.json");
Path fcPath = Paths.get(fcFile.toURI());
// test with full reading
GeoJSONObject geoJSONObject = GeoJSONParser.parse(fcPath, false);
assertTrue(geoJSONObject instanceof GeoJSONFeatureCollection);
GeoJSONFeatureCollection geojsonFC = (GeoJSONFeatureCollection) geoJSONObject;
assertFalse(geojsonFC.isLazyMode());
assertEquals(7, geojsonFC.getFeatures().size());
for (int i = 0; i < 7; i++) {
assertTrue(geojsonFC.hasNext());
assertNotNull(geojsonFC.next());
}
// end of collection
assertFalse(geojsonFC.hasNext());
// test in lazy reading
geoJSONObject = GeoJSONParser.parse(fcPath, true);
assertTrue(geoJSONObject instanceof GeoJSONFeatureCollection);
geojsonFC = (GeoJSONFeatureCollection) geoJSONObject;
assertTrue(geojsonFC.isLazyMode());
// lazy don't know number of features
assertEquals(0, geojsonFC.getFeatures().size());
for (int i = 0; i < 7; i++) {
assertTrue(geojsonFC.hasNext());
assertNotNull(geojsonFC.next());
}
// end of collection
assertFalse(geojsonFC.hasNext());
}
Aggregations