Inserting Data
Function Description
You can construct data and insert data into the specified table.
Sample Code
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
String id;
Object[] NO_VALUES = {};
String[] PEOPLE_NAMES = {"Addams", "Bierce", "Clemens"};
Long SECONDS_PER_YEAR = 365L * 24L * 60L * 60L;
Random random = new Random(5771);
DateTime MIN_DATE = new DateTime(2014, 1, 1, 0, 0, 0, DateTimeZone.forID("UTC"));
Double MIN_X = -79.5;
Double MIN_Y = 37.0;
Double DX = 2.0;
Double DY = 2.0;
for (int i = 0; i < numNewFeatures; i++) {
// create the new (unique) identifier and empty feature shell
id = "Observation." + Integer.toString(i);
SimpleFeature simpleFeature = SimpleFeatureBuilder.build(simpleFeatureType, NO_VALUES, id);
// be sure to tell GeoTools explicitly that you want to use the ID you provided
simpleFeature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);
// populate the new feature's attributes
// Who: string value
simpleFeature.setAttribute("Who", PEOPLE_NAMES[i % PEOPLE_NAMES.length]);
// What: long value
simpleFeature.setAttribute("What", i);
// Where: location: construct a random point within a 2-degree-per-side square
double x = MIN_X + random.nextDouble() * DX;
double y = MIN_Y + random.nextDouble() * DY;
Geometry geometry = WKTUtils$.MODULE$.read("POINT(" + x + " " + y + ")");
simpleFeature.setAttribute("Where", geometry);
// When: date-time:construct a random instant within a year
DateTime dateTime = MIN_DATE.plusSeconds((int) Math.round(random.nextDouble() * SECONDS_PER_YEAR));
simpleFeature.setAttribute("When", dateTime.toDate());
// Why: another string value
// left empty, showing that not all attributes need values
// accumulate this new feature in the collection
featureCollection.add(simpleFeature);
FeatureStore featureStore = (FeatureStore) dataStore.getFeatureSource(simpleFeatureTypeName);
featureStore.addFeatures(featureCollection);
} Last Article: Creating a Schema
Next Article: Querying Data
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.