After creating an empty table, as explained in Create an empty Informix Spatial table, you can start adding spatial data to the table.
You can load data from and external file — a JSON file in this example — and add the elements to the table.
Start by decoding the GeoJSON data into an ILcdModel
using an ILcdModelDecoder
.
static ILcdModel loadJsonModel() throws IOException {
TLcdGeoJsonModelDecoder decoder = new TLcdGeoJsonModelDecoder();
return decoder.decode("Data/geojson/world.geojson");
}
You must also decode the content of the table you want to add the GeoJSON data to.
static TLcdDatabaseModel loadDatabaseModel() throws IOException {
TLcdInformixSpatialModelDecoder decoder = new TLcdInformixSpatialModelDecoder();
return (TLcdDatabaseModel) decoder.decode(getProperties());
}
To decode the Informix Spatial table, you can use the same properties as the ones used in the Create an empty Informix Spatial table article.
After decoding both ILcdModels
, take these steps for all elements in the GeoJSON model:
-
Create a
TLcdFeaturedSingleShapeList
with the features and data type of theTLcdDatabaseModelDescriptor
of theTLcdDatabaseModel
and theILcdShape
of the element of the GeoJSON model. -
Copy the values of the properties of the element of the JSON model to the newly created
TLcdFeaturedSingleShapeList
. -
Add the element to the
TLcdDatabaseModel
.
Finally, commit
the changes.
ILcdModel jsonModel = loadJsonModel();
TLcdDatabaseModel databaseModel = loadDatabaseModel();
TLcdDatabaseModelDescriptor databaseModelModelDescriptor = (TLcdDatabaseModelDescriptor) databaseModel.getModelDescriptor();
// Retrieve features and datatype of database model.
Object[] features = IntStream.range(0, databaseModelModelDescriptor.getFeatureCount()).mapToObj(databaseModelModelDescriptor::getFeatureName).toArray();
TLcdDataType dataType = databaseModelModelDescriptor.getModelElementType();
for (Object elementOfJsonModel : jsonModel.query(ILcdModel.all()).toList()) {
// Retrieve the shape of the element of the json model and use it to construct an element for the database model.
ILcdShape shape = ALcdShape.fromDomainObject(elementOfJsonModel);
TLcdDataObjectShapeList element = new TLcdDataObjectShapeList(dataType, shape);
// Copy the values of the properties of the element of the json model to the newly constructed element of the database model.
// The featureNames (and there type) of the database model need to match those of the json model for this to work.
for (TLcdDataProperty property : dataType.getProperties()) {
element.setValue(property, ((ILcdDataObject) elementOfJsonModel).getValue(property.getName()));
}
databaseModel.addElement(element, ILcdModel.NO_EVENT);
}
databaseModel.fireCollectedModelChanges();
// Save the changes to the database model.
databaseModel.commit();
The |