@LcdService(service=ILcdModelDecoder.class, priority=20000) public class TLcdGeoJsonModelDecoder extends Object implements ILcdModelDecoder, ILcdInputStreamFactoryCapable
FeatureCollection
to a Luciad ILcdModel
.
The decoder only accepts source-paths ending with the extension .json, .geojson, or .js.
The input file must contain a single FeatureCollection. See the geojson specification for more details.
A FeatureCollection must at least contain 2 members:
type
: with a value equal to "FeatureCollection"features
: an array containing zero or more domain objects. In turn, each Feature
must at least have:
type
: with a value equal to "Feature"geometry
: a GeoJson geometryproperties
: properties (as key-value pairs) of the featureid (optional)
: a unique identifierA valid GeoJson FeatureCollection containing a single point feature.
{
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [4.668864,50.864871],
},
properties: {
poi: "Luciad NV HQ"
}
}
]
}
input stream factory
of this decoder.
ILcd2DBoundsIndexedModel
.TLcdGeoJsonModelDescriptor
.
ILcdShapeList
and ILcdDataObject
.
Point
: decoded as a ILcdShapeList
containing a single
ILcdPoint
LineString
: decoded as a ILcdShapeList
containing a single
ILcdPolyline
Polygon
: decoded as a ILcdShapeList
ILcdPolygon
when the polygon is defined by a single
outlineILcdComplexPolygon
when the polygon is defined by an outline
and one or more interior rings.MultiPoint
: decoded as a ILcdShapeList
containing a single
ILcdPolyPoint
MultiLineString
: decoded as a ILcdShapeList
containing multiple
ILcdPolyline
MultiPolygon
: decoded as a ILcdShapeList
containing a combination
of
ILcdPolygon
when a polygon consist only of a single outlineILcdComplexPolygon
when a polygon consist only of a single outline and one or
more interiour ringsGeometryCollection
: decoded as a ILcdShapeList
containing a
combination of zero or more ILcdPoint
, ILcdPolypoint
,
ILcdPolyLine
, ILcdPolygon
, ILcdComplexPolygon
. The properties of the GeoJson feature can be accessed through the methods of the
ILcdDataObject
interface.
It is typically the case that all features in a GeoJson FeatureCollection share the same
properties. However, this is not required.
The decoder will construct a single ILcdDataType
which is shared by all Features in
the collection.
Below are some typical examples.
Example 1: the ILcdDataType of the features in this collection has 2 properties: "poi" and "country".
{
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [4.668864,50.864871]
},
properties: {
poi: "Luciad NV HQ",
country: "Belgium"
}
},
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [-77.340020,38.946660],
},
properties: {
poi: "Luciad Inc HQ",
country: "United States of America"
}
}
]
}
Example 2: the ILcdDataType of the features in this collection have 3 properties: "poi",
"country" and "state". The value of the "state" property of the first feature is
null
.
{
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [4.668864,50.864871],
properties: {
poi: "Luciad NV HQ",
country: "Belgium"
}
}
},
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [-77.340020,38.946660],
properties: {
poi: "Luciad Inc HQ",
country: "United States of America",
state: "VA"
}
}
}
]
}
String primaryKeyPropertyName = null;
TLcdPrimaryKeyAnnotation annotation = aDataType.getAnnotation(TLcdPrimaryKeyAnnotation.class);
if(annotation != null){
primaryKeyPropertyName = annotation.getProperty().getName();
}
It is often useful to identify the model with an appropriate name. For example, you might have other components in your code that - depending on a given model - perform different actions.
You can configure the name of the data model and the name of the data type explicitly using
setDataModelName
setDataTypeName
When these are not set, default values based on the name of the model source will be used.
Property names must be a string
.
{
"this is a string": "good property name",
thisIsAString: "also a good property name",
0: "bad property name. This json is illegal and cannot be decoded."
}
For details about the Json format, please visit json.org
A value in Json can only be one of these types: String
,
number
,
object
(a collection of key-value pairs), array
(a list of values),
true
, false
, and null
.
These types are mapped to a TLcdDataType
(in the case of primitives)
or a TLcdDataProperty.CollectionType
(in the case of collections) in
the following manner:
String
: TLcdCoreDataTypes.STRING_TYPE
Number
:
TLcdCoreDataTypes.INTEGER_TYPE
: if the number has no decimal point.TLcdCoreDataTypes.LONG_TYPE
: if the number has no decimal point and does not fit in a 32 bit
integer.TLcdCoreDataTypes.DOUBLE_TYPE
: if the number has a decimal point. true/false
: TLcdCoreDataTypes.BOOLEAN_TYPEarray
: TLcdDataProperty.CollectionType.LIST.object
: TLcdDataProperty.CollectionType.MAP.Example 3: An example GeoJson file with a valid named spatial reference.
{
type: "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [4.668864,50.864871],
properties: {
poi: "Luciad NV HQ",
country: "Belgium"
}
}
},
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [-77.340020,38.946660],
properties: {
poi: "Luciad Inc HQ",
country: "United States of America",
state: "VA"
}
}
}
]
}
ILcdModelDecoder decoder = new TLcdGeoJsonModelDecoder();
ILcdModel model = decoder.decode("world.geojson");
read lock
.Constructor and Description |
---|
TLcdGeoJsonModelDecoder()
Create a new model decoder for GeoJson sources.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canDecodeSource(String aSourceName)
The method will return
true only when aSourceName ends in .geojson,
.json, or .js. |
ILcdModel |
decode(String aSourceName)
Creates a new model from the source.
|
static ILcdDataObject |
decodeFeature(InputStream aGeoJsonFeatureInputStream,
ILcdModelReference aModelReference,
TLcdDataType aDataType)
Decodes a geojson string to a data object.
|
String |
getDataModelName()
Gets the data model name.
|
TLcdDataType |
getDataType()
Get the user provided data type.
|
String |
getDataTypeName()
Gets the data type name.
|
ILcdModelReference |
getDefaultModelReference()
Retrieve the default model reference.
|
String |
getDisplayName()
A short label for the format.
|
ILcdInputStreamFactory |
getInputStreamFactory()
Returns the input stream factory that is used.
|
ILcdModelReferenceParser |
getModelReferenceParser()
Get the reference parser that is used by this decoder.
|
protected ILcdShape |
parseCustomGeometry(String aJsonNode,
ILcdModelReference aModelReference)
This method is used by the decoder to parse custom geometry that is normally not allowed in GeoJSON.
|
static ILcdShape |
parseGeometry(InputStream aInputStream,
ILcdModelReference aModelReference)
Parses a single GeoJson geometry object to an
ILcdShape . |
void |
setDataModelName(String aDataModelName)
Sets the data model name.
|
void |
setDataTypeName(String aDataTypeName)
Sets the data type name used by the decoder.
|
void |
setDefaultModelReference(ILcdModelReference aModelReference)
Sets the default model reference.
|
void |
setInputStreamFactory(ILcdInputStreamFactory aInputStreamFactory)
Sets the input stream factory to be used.
|
void |
setModelElementType(TLcdDataType aDataType)
Sets a user provided data type.
|
void |
setModelReferenceParser(ILcdModelReferenceParser aModelReferenceParser)
Set the reference parser the decoder will use to parse a spatial reference.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
canDecodeSource, decodeModelMetadata, decodeModelMetadata, decodeSource, discoverDataSources
public TLcdGeoJsonModelDecoder()
TLcdGeoJsonModelDecoder myGeoJsonModelDecoder = new TLcdGeoJsonModelDecoder();
public void setModelElementType(TLcdDataType aDataType)
setDataModelName
and setDataTypeName
are mutually exclusive. The data model name and type name of aDataType
have
priority over any names that were set with the setDataModelName
or
setDataTypeName
methods.aDataType
- the data type to use when decoding. It may only contain properties of the primitive types
INTEGER_TYPE
, LONG_TYPE
, FLOAT_TYPE
,
DOUBLE_TYPE
, NUMBER_TYPE
, STRING_TYPE
,
OBJECT_TYPE
, STRING_TYPE
, BOOLEAN_TYPE
,
STRING_TYPE
, OBJECT_TYPE
or of the collection types
MAP
and LIST
.public TLcdDataType getDataType()
null
.public String getDisplayName()
getDisplayName
in interface ILcdModelDecoder
public boolean canDecodeSource(String aSourceName)
true
only when aSourceName ends in .geojson,
.json, or .js.canDecodeSource
in interface ILcdModelDecoder
aSourceName
- the data source to be verified; typically a file name or a URL.ILcdModelDecoder.decode(String)
,
ILcdModelDecoder.decodeModelMetadata(String)
public ILcdModel decode(String aSourceName) throws IOException
Creates a new model from the source. This model implements ILcdModel
.
The source must contain a valid GeoJson FeatureCollection. For convenience, it will also support
a single GeoJson Feature, a single GeoJson Geometry, or a an array of GeoJson features.
To decode a single feature, note that you can also use the static decodeFeature
method.
decode
in interface ILcdModelDecoder
aSourceName
- the data source to be decoded; typically a file name or a URL.IOException
InterruptedIOException
- When the thread on which this method is called is interrupted: it is recommended to stop the decoding
and throw an InterruptedIOException
.
This same exception can also be used if the decoder shows UI to the user, and the user cancels the decoding
through the UI.ILcdModelDecoder.canDecodeSource(String)
public void setInputStreamFactory(ILcdInputStreamFactory aInputStreamFactory)
ILcdInputStreamFactoryCapable
setInputStreamFactory
in interface ILcdInputStreamFactoryCapable
aInputStreamFactory
- the input stream factory to be used.public ILcdInputStreamFactory getInputStreamFactory()
ILcdInputStreamFactoryCapable
getInputStreamFactory
in interface ILcdInputStreamFactoryCapable
public ILcdModelReferenceParser getModelReferenceParser()
public void setModelReferenceParser(ILcdModelReferenceParser aModelReferenceParser)
aModelReferenceParser
- The reference parser for the decoder to use.public void setDefaultModelReference(ILcdModelReference aModelReference)
aModelReference
- the model reference used as a default, or null
if no custom default model
reference should be set.public ILcdModelReference getDefaultModelReference()
null
if no custom default model reference is set.public void setDataTypeName(String aDataTypeName)
setModelElementType
.aDataTypeName
- the data type namepublic String getDataTypeName()
public void setDataModelName(String aDataModelName)
null
. If this value is null, the
file-name will be used for the data model name. Note that this name is not used if the user has
set the data type explicitly with setModelElementType
.aDataModelName
- the data model namepublic String getDataModelName()
public static ILcdDataObject decodeFeature(InputStream aGeoJsonFeatureInputStream, ILcdModelReference aModelReference, TLcdDataType aDataType) throws IOException
TLcdGeoJsonModelEncoder.exportFeature(Object, java.io.OutputStream)
static method for the reverse operation.
Note: When using this method to decode a feature, parseCustomGeometry(String, ILcdModelReference)
is not called.aGeoJsonFeatureInputStream
- the json feature. It must be a stream for a UTF-8 string.aModelReference
- the model reference of the feature.
If this is null
,
the WGS86 geodetic reference is used.aDataType
- the data type of the feature.IOException
- Thrown when the feature cannot be decoded. This is often caused by invalid
GeoJson.public static ILcdShape parseGeometry(InputStream aInputStream, ILcdModelReference aModelReference) throws IOException
ILcdShape
.
The following table shows which geometries are parsed to which shape types:
Geometry | Parsed to |
---|---|
Point |
An ILcdPoint |
MultiPoint |
An ILcdShapeList containing zero or more ILcdPoint instances |
LineString |
An ILcdPolyline |
MultiLineString |
An ILcdShapeList containing zero or more ILcdPolyline instances |
Polygon |
An ILcdPolygon if the polygon consists of only one ring, or an ILcdComplexPolygon otherwise |
MultiPolygon |
An ILcdShapeList containing zero or more ILcdPolygon or ILcdComplexPolygon instances |
GeometryCollection |
An ILcdShapeList containing any combination of above-mentioned shapes. |
parseCustomGeometry(String, ILcdModelReference)
is not called.aInputStream
- the GeoJson geometry object to parse.aModelReference
- the model reference in which the geometry is defined.IOException
- If the parsing fails.protected ILcdShape parseCustomGeometry(String aJsonNode, ILcdModelReference aModelReference) throws IOException
IOException
.aJsonNode
- the json node representing the custom geometryaModelReference
- the model referenceIOException
.IOException