use of org.hortonmachine.gui.utils.GuiBridgeHandler in project hortonmachine by TheHortonMachine.
the class SqlTemplatesAndActions method doTiles.
private static void doTiles(GuiBridgeHandler guiBridge, DatabaseViewer databaseViewer, boolean isRaster) {
try {
String label = isRaster ? "raster" : "vector";
FileFilter fileFilter = isRaster ? HMConstants.rasterFileFilter : HMConstants.vectorFileFilter;
String title = "Open " + label + " file";
File[] openFiles = GuiUtilities.showOpenFilesDialog(databaseViewer, title, true, PreferencesHandler.getLastFile(), fileFilter);
if (openFiles != null && openFiles.length > 0) {
try {
PreferencesHandler.setLastPath(openFiles[0].getAbsolutePath());
} catch (Exception e1) {
logger.insertError("SqlTemplatesAndActions", "ERROR", e1);
}
} else {
return;
}
try {
String targetEpsg = "epsg:" + GeopackageCommonDb.MERCATOR_SRID;
CoordinateReferenceSystem mercatorCrs = CrsUtilities.getCrsFromEpsg(targetEpsg, null);
GeopackageCommonDb db = (GeopackageCommonDb) databaseViewer.currentConnectedSqlDatabase;
List<FeatureEntry> features4326 = db.features();
PreparedGeometry limitsGeom3857 = null;
if (features4326.size() > 0) {
List<String> names = features4326.stream().map(f -> f.getTableName()).collect(Collectors.toList());
String selectedTable = GuiUtilities.showComboDialog(databaseViewer, "Area of interest", "It is possible to use one of the vector tables as area of interest. Tiles outside the area will be ignored.", names.toArray(new String[0]), "");
if (selectedTable != null) {
SqlName tName = SqlName.m(selectedTable);
GeometryColumn gc = db.getGeometryColumnsForTable(tName);
List<Geometry> geometries = db.getGeometriesIn(tName, (Envelope) null, (String[]) null);
int dataSrid = db.feature(tName).getSrid();
List<Geometry> geometries3857;
if (dataSrid == GeopackageCommonDb.MERCATOR_SRID) {
geometries3857 = geometries;
} else {
String sourceEpsg = "epsg:" + dataSrid;
CoordinateReferenceSystem sourceCRS = CrsUtilities.getCrsFromEpsg(sourceEpsg, null);
;
MathTransform transform = CRS.findMathTransform(sourceCRS, mercatorCrs);
geometries3857 = geometries.stream().map(g -> {
try {
return JTS.transform(g, transform);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}).collect(Collectors.toList());
}
if (gc.geometryType.isPolygon()) {
// use polygons
Geometry union = CascadedPolygonUnion.union(geometries3857);
limitsGeom3857 = PreparedGeometryFactory.prepare(union);
} else {
// use envelopes
Envelope env = new Envelope();
for (Geometry geometry : geometries3857) {
env.expandToInclude(geometry.getEnvelopeInternal());
}
Polygon polyEnv = GeometryUtilities.createPolygonFromEnvelope(env);
limitsGeom3857 = PreparedGeometryFactory.prepare(polyEnv);
}
}
}
String nameForTable = FileUtilities.getNameWithoutExtention(openFiles[0]);
String[] zoomLevels = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19" };
HashMap<String, String[]> fields2ValuesMap = new HashMap<>();
String newTableLabel = "name of the new table";
String minZoomLevelLabel = "min zoom level";
String maxZoomLevelLabel = "max zoom level";
fields2ValuesMap.put(minZoomLevelLabel, zoomLevels);
fields2ValuesMap.put(maxZoomLevelLabel, zoomLevels);
String[] result = //
GuiUtilities.showMultiInputDialog(//
databaseViewer, //
"Select parameters", //
new String[] { newTableLabel, minZoomLevelLabel, maxZoomLevelLabel }, //
new String[] { nameForTable, "8", "16" }, null);
if (result == null) {
return;
}
PreparedGeometry _limitsGeom3857 = limitsGeom3857;
final LogConsoleController logConsole = new LogConsoleController(null);
IHMProgressMonitor pm = logConsole.getProgressMonitor();
Logger.INSTANCE.setOutPrintStream(logConsole.getLogAreaPrintStream());
Logger.INSTANCE.setErrPrintStream(logConsole.getLogAreaPrintStream());
guiBridge.showWindow(logConsole.asJComponent(), "Console Log");
new Thread(() -> {
try {
logConsole.beginProcess("Import " + label + " to tileset");
pm.message("Checking input parameters...");
String _nameForTable = result[0];
int minZoom = 8;
int maxZoom = 16;
try {
minZoom = Integer.parseInt(result[1]);
maxZoom = Integer.parseInt(result[2]);
} catch (Exception e1) {
pm.errorMessage("The min or max zoomlevel were not entered correctly, exiting.");
return;
}
for (File file : openFiles) {
String dataPath = file.getAbsolutePath();
Envelope envelopeInternal = null;
if (isRaster) {
AbstractGridFormat format = GridFormatFinder.findFormat(file);
AbstractGridCoverage2DReader reader = format.getReader(file, null);
GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope();
CoordinateReferenceSystem crs = reader.getCoordinateReferenceSystem();
double[] ll = originalEnvelope.getLowerCorner().getCoordinate();
double[] ur = originalEnvelope.getUpperCorner().getCoordinate();
ReferencedEnvelope renv = new ReferencedEnvelope(ll[0], ur[0], ll[1], ur[1], crs);
envelopeInternal = renv.transform(mercatorCrs, true);
} else {
ReferencedEnvelope renv = OmsVectorReader.readEnvelope(file.getAbsolutePath());
envelopeInternal = renv.transform(mercatorCrs, true);
}
ITilesProducer tileProducer = new GeopackageTilesProducer(pm, dataPath, isRaster, minZoom, maxZoom, 256, _limitsGeom3857);
String description = "HM import of " + openFiles[0].getName();
int addedTiles = ((GeopackageCommonDb) databaseViewer.currentConnectedSqlDatabase).addTilestable(SqlName.m(_nameForTable), description, envelopeInternal, tileProducer);
pm.message("Inserted " + addedTiles + " new tiles.");
}
databaseViewer.refreshDatabaseTree();
} catch (Exception ex) {
pm.errorMessage(ex.getLocalizedMessage());
} finally {
logConsole.finishProcess();
logConsole.stopLogging();
Logger.INSTANCE.resetStreams();
}
}, "DatabaseController->Import " + label + " to tileset").start();
} catch (Exception e1) {
GuiUtilities.handleError(databaseViewer, e1);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations