After we have created an empty table as explained in Create empty Oracle table, we 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");
}
We also need to decode the content of the table we want to add the geojon data to.
static TLcdDatabaseModel loadDatabaseModel() throws IOException {
TLcdOracleSpatialModelDecoder decoder = new TLcdOracleSpatialModelDecoder();
return (TLcdDatabaseModel) decoder.decode(getProperties());
}
The properties used when decoding the Oracle table are the same as we used in the Create empty Oracle 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 |