Select to view content in your preferred language

No data error GeoDatabase

386
3
07-11-2024 01:37 AM
rod182211
Emerging Contributor

For the life of e I can't figure this out any ideas. Here is my code. Obviously something silly. Java 17 200.4 

String GEO_DATABASE_FOLDER = System.getProperty("user.home") +
System.getProperty("file.separator") + "." + APP_NAME.toLowerCase() +
System.getProperty("file.separator") + DATA_BASEDIRECTORY +
System.getProperty("file.separator") + "geodatabase.geodatabase";
package com.neoplexus.database;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.*;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;

import static com.neoplexus.iris.Constants.GEO_DATABASE_FOLDER;

public class GeoDataBase {

private Geodatabase db;
private GeodatabaseFeatureTable trackFeatureTable;
private GeodatabaseFeatureTable lineFeatureTable;
private GeodatabaseFeatureTable areaFeatureTable;
private GeodatabaseFeatureTable multipointFeatureTable;
private FeatureLayer trackFeatureLayer;
private FeatureLayer lineFeatureLayer;
private FeatureLayer areaFeatureLayer;
private FeatureLayer multipointFeatureLayer;
private ExecutorService executorService = Executors.newFixedThreadPool(4);
private boolean hasAlt;

public void initializeGeodatabase() {
System.out.println("Geodatabase path: " + GEO_DATABASE_FOLDER);

executorService.submit(() -> {
// Ensure the directory structure exists
File geodatabaseFile = new File(GEO_DATABASE_FOLDER);
File parentDir = geodatabaseFile.getParentFile();

if (!parentDir.exists() && !parentDir.mkdirs()) {
System.err.println("Failed to create directories: " + parentDir.getAbsolutePath());
return;
}

// Check if the geodatabase file exists
if (!geodatabaseFile.exists()) {
try {
if (geodatabaseFile.createNewFile()) {
System.out.println("Geodatabase file created: " + geodatabaseFile.getAbsolutePath());
} else {
System.err.println("Failed to create geodatabase file.");
return;
}
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
return;
}
} else {
System.out.println("Geodatabase file already exists: " + geodatabaseFile.getAbsolutePath());
}

// Initialize and load the geodatabase
db = new Geodatabase(geodatabaseFile.getAbsolutePath());
db.loadAsync();
db.addDoneLoadingListener(() -> {
if (db.getLoadStatus() == LoadStatus.LOADED) {
System.out.println("Geodatabase loaded successfully.");
createAndLoadFeatureTables();
} else {
System.err.println("Failed to load geodatabase.");
Throwable loadError = db.getLoadError();
if (loadError != null) {
System.err.println("Load error: " + loadError.getMessage());
loadError.printStackTrace();
}
}
});
});
}

private void createAndLoadFeatureTables() {
createGeodatabaseFeatureTable("TRACK_TABLE", GeometryType.POINT, true).thenAccept(table -> {
if (table != null) {
trackFeatureTable = table;
trackFeatureLayer = createFeatureLayer(trackFeatureTable);
System.out.println("Created and loaded TRACK_TABLE");
} else {
System.err.println("Failed to create TRACK_TABLE");
}
});

createGeodatabaseFeatureTable("LINE_TABLE", GeometryType.POLYLINE, true).thenAccept(table -> {
if (table != null) {
lineFeatureTable = table;
lineFeatureLayer = createFeatureLayer(lineFeatureTable);
System.out.println("Created and loaded LINE_TABLE");
} else {
System.err.println("Failed to create LINE_TABLE");
}
});

createGeodatabaseFeatureTable("AREA_TABLE", GeometryType.POLYGON, true).thenAccept(table -> {
if (table != null) {
areaFeatureTable = table;
areaFeatureLayer = createFeatureLayer(areaFeatureTable);
System.out.println("Created and loaded AREA_TABLE");
} else {
System.err.println("Failed to create AREA_TABLE");
}
});

createGeodatabaseFeatureTable("MULTIPOINT_TABLE", GeometryType.MULTIPOINT, true).thenAccept(table -> {
if (table != null) {
multipointFeatureTable = table;
multipointFeatureLayer = createFeatureLayer(multipointFeatureTable);
System.out.println("Created and loaded MULTIPOINT_TABLE");
} else {
System.err.println("Failed to create MULTIPOINT_TABLE");
}
});

db.getGeodatabaseFeatureTables().forEach(table -> {
table.loadAsync();
table.addDoneLoadingListener(() -> {
if (table.getLoadStatus() == LoadStatus.LOADED) {
System.out.println("Loaded feature table: " + table.getTableName());
} else {
System.err.println("Failed to load feature table: " + table.getTableName());
}
});
});
}

private FeatureLayer createFeatureLayer(GeodatabaseFeatureTable table) {
FeatureLayer featureLayer = new FeatureLayer(table);
featureLayer.loadAsync();
featureLayer.addDoneLoadingListener(() -> {
if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
System.out.println("Feature layer loaded: " + table.getTableName());
} else {
System.err.println("Failed to load feature layer: " + table.getTableName());
}
});
return featureLayer;
}

private CompletableFuture<GeodatabaseFeatureTable> createGeodatabaseFeatureTable(String name, GeometryType geoType, boolean hasAlt) {
return CompletableFuture.supplyAsync(() -> {
TableDescription tableDescription = createTableDescription(name, geoType, hasAlt);

ListenableFuture<GeodatabaseFeatureTable> futureTable = db.createTableAsync(tableDescription);
try {
GeodatabaseFeatureTable table = futureTable.get();
System.out.println("Created Feature Table: " + table.getTableName());
return table;
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
}
}, executorService);
}

private TableDescription createTableDescription(String name, GeometryType geomType, boolean hasAlt) {
TableDescription tableDescription = new TableDescription(name, SpatialReference.create(4326), geomType);

List<FieldDescription> fieldDescriptions = List.of(
new FieldDescription("MMSI", Field.Type.TEXT),
new FieldDescription("controlPoints", Field.Type.TEXT),
new FieldDescription("wkid", Field.Type.TEXT),
new FieldDescription("uniquedesignation", Field.Type.TEXT),
new FieldDescription("sidc", Field.Type.TEXT),
new FieldDescription("indicator", Field.Type.TEXT),
new FieldDescription("MyNotes", Field.Type.TEXT),
new FieldDescription("x", Field.Type.DOUBLE),
new FieldDescription("z", Field.Type.DOUBLE),
new FieldDescription("z2", Field.Type.DOUBLE),
new FieldDescription("y", Field.Type.DOUBLE),
new FieldDescription("distance", Field.Type.DOUBLE),
new FieldDescription("direction", Field.Type.DOUBLE),
new FieldDescription("speed", Field.Type.DOUBLE),
new FieldDescription("datetimevalid", Field.Type.TEXT),
new FieldDescription("createdby", Field.Type.TEXT),
new FieldDescription("datetimeexpired", Field.Type.TEXT),
new FieldDescription("datecreated", Field.Type.TEXT)
);

tableDescription.getFieldDescriptions().addAll(fieldDescriptions);
tableDescription.setHasAttachments(false);
tableDescription.setHasM(false);
tableDescription.setHasZ(hasAlt);

return tableDescription;
}

public void addFeatureToTrackTable(Point point, HashMap<String, Object> attributes) {
if (trackFeatureTable.getLoadStatus() != LoadStatus.LOADED) {
System.err.println("Feature table is not loaded.");
return;
}

Feature feature = trackFeatureTable.createFeature(attributes, point);
ListenableFuture<Void> addFeatureFuture = trackFeatureTable.addFeatureAsync(feature);
addFeatureFuture.addDoneListener(() -> {
try {
addFeatureFuture.get();
System.out.println("Feature added to TRACK_TABLE: " + feature.getAttributes());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
}
}
0 Kudos
3 Replies
rod182211
Emerging Contributor

I tried to minimize to this but still no luck 

package com.neoplexus.database;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.*;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;

import static com.neoplexus.iris.Constants.GEO_DATABASE_FOLDER;

public class GeoDataBase {

private Geodatabase db;
private GeodatabaseFeatureTable trackFeatureTable;
private FeatureLayer trackFeatureLayer;
private ExecutorService executorService = Executors.newFixedThreadPool(4);

public void initializeGeodatabase() {
System.out.println("Geodatabase path: " + GEO_DATABASE_FOLDER);

executorService.submit(() -> {
// Ensure the directory structure exists
File geodatabaseFile = new File(GEO_DATABASE_FOLDER);
File parentDir = geodatabaseFile.getParentFile();

if (!parentDir.exists() && !parentDir.mkdirs()) {
System.err.println("Failed to create directories: " + parentDir.getAbsolutePath());
return;
}

// Check if the geodatabase file exists
if (!geodatabaseFile.exists()) {
try {
if (geodatabaseFile.createNewFile()) {
System.out.println("Geodatabase file created: " + geodatabaseFile.getAbsolutePath());
createGeodatabase(geodatabaseFile.getAbsolutePath());
} else {
System.err.println("Failed to create geodatabase file.");
return;
}
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
return;
}
} else {
System.out.println("Geodatabase file already exists: " + geodatabaseFile.getAbsolutePath());
loadGeodatabase(geodatabaseFile.getAbsolutePath());
}
});
}

private void createGeodatabase(String geodatabasePath) {
db = new Geodatabase(geodatabasePath);
db.loadAsync();
db.addDoneLoadingListener(() -> {
if (db.getLoadStatus() == LoadStatus.LOADED) {
createAndLoadFeatureTables();
} else {
handleLoadError();
}
});
}

private void loadGeodatabase(String geodatabasePath) {
db = new Geodatabase(geodatabasePath);
db.loadAsync();
db.addDoneLoadingListener(() -> {
if (db.getLoadStatus() == LoadStatus.LOADED) {
trackFeatureTable = findFeatureTableByName("TRACK_TABLE");

db.getGeodatabaseFeatureTables().forEach(table -> {
table.loadAsync();
table.addDoneLoadingListener(() -> {
if (table.getLoadStatus() != LoadStatus.LOADED) {
System.err.println("Failed to load feature table: " + table.getTableName());
}
});
});
initializeFeatureLayers();
} else {
handleLoadError();
}
});
}

private GeodatabaseFeatureTable findFeatureTableByName(String tableName) {
return db.getGeodatabaseFeatureTables().stream()
.filter(table -> table.getTableName().equals(tableName))
.findFirst().orElse(null);
}

private void initializeFeatureLayers() {
if (trackFeatureTable != null) {
trackFeatureLayer = createFeatureLayer(trackFeatureTable);
}
}

private void createAndLoadFeatureTables() {
CompletableFuture<GeodatabaseFeatureTable> futureTrackTable = createGeodatabaseFeatureTable("TRACK_TABLE");
futureTrackTable.thenAccept(table -> {
if (table != null) {
trackFeatureTable = table;
trackFeatureLayer = createFeatureLayer(trackFeatureTable);
}
});

db.getGeodatabaseFeatureTables().forEach(table -> {
table.loadAsync();
table.addDoneLoadingListener(() -> {
if (table.getLoadStatus() != LoadStatus.LOADED) {
System.err.println("Failed to load feature table: " + table.getTableName());
}
});
});
}

private FeatureLayer createFeatureLayer(GeodatabaseFeatureTable table) {
FeatureLayer featureLayer = new FeatureLayer(table);
featureLayer.loadAsync();
featureLayer.addDoneLoadingListener(() -> {
if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
System.out.println("Feature layer loaded: " + table.getTableName());
} else {
System.err.println("Failed to load feature layer: " + table.getTableName());
}
});
return featureLayer;
}

private CompletableFuture<GeodatabaseFeatureTable> createGeodatabaseFeatureTable(String name) {
return CompletableFuture.supplyAsync(() -> {
TableDescription tableDescription = createTableDescription(name);

ListenableFuture<GeodatabaseFeatureTable> futureTable = db.createTableAsync(tableDescription);
try {
GeodatabaseFeatureTable table = futureTable.get();
System.out.println("Created Feature Table: " + table.getTableName());
return table;
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
}
}, executorService);
}

private TableDescription createTableDescription(String name) {
TableDescription tableDescription = new TableDescription(name);

List<FieldDescription> fieldDescriptions = List.of(
new FieldDescription("MMSI", Field.Type.TEXT),
new FieldDescription("controlPoints", Field.Type.TEXT),
new FieldDescription("wkid", Field.Type.TEXT),
new FieldDescription("uniquedesignation", Field.Type.TEXT),
new FieldDescription("sidc", Field.Type.TEXT),
new FieldDescription("indicator", Field.Type.TEXT),
new FieldDescription("MyNotes", Field.Type.TEXT),
new FieldDescription("x", Field.Type.DOUBLE),
new FieldDescription("z", Field.Type.DOUBLE),
new FieldDescription("z2", Field.Type.DOUBLE),
new FieldDescription("y", Field.Type.DOUBLE),
new FieldDescription("distance", Field.Type.DOUBLE),
new FieldDescription("direction", Field.Type.DOUBLE),
new FieldDescription("speed", Field.Type.DOUBLE),
new FieldDescription("datetimevalid", Field.Type.TEXT),
new FieldDescription("createdby", Field.Type.TEXT),
new FieldDescription("datetimeexpired", Field.Type.TEXT),
new FieldDescription("datecreated", Field.Type.TEXT)
);

tableDescription.getFieldDescriptions().addAll(fieldDescriptions);
tableDescription.setHasAttachments(false);

return tableDescription;
}

private void handleLoadError() {
System.err.println("Failed to load geodatabase.");
Throwable loadError = db.getLoadError();
if (loadError != null) {
System.err.println("Load error: " + loadError.getMessage());
loadError.printStackTrace();
}
}

public void addFeatureToTrackTable(Point point, HashMap<String, Object> attributes) {
if (trackFeatureTable.getLoadStatus() != LoadStatus.LOADED) {
System.err.println("Feature table is not loaded.");
return;
}

Feature feature = trackFeatureTable.createFeature(attributes, point);
ListenableFuture<Void> addFeatureFuture = trackFeatureTable.addFeatureAsync(feature);
addFeatureFuture.addDoneListener(() -> {
try {
addFeatureFuture.get();
System.out.println("Feature added to TRACK_TABLE: " + feature.getAttributes());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
}
}

 

0 Kudos
MarkBaird
Esri Regular Contributor

@rod182211 can you add the code which shows how you are using the methods in that class.  Can you also indicate where you are seeing the error?

I suspect also I'm going to need the database file to reproduce this.  Is there a way you can get it to me?  I think you've been in email contact with me or Kerry previously.

 

0 Kudos
rod182211
Emerging Contributor

Hi Mark, Thanks for the reply. I did a work around by creating the file in Arcgis Pro first. This seemed to work. It looks like the schema is not being initially developed.

0 Kudos