use of org.hortonmachine.dbs.datatypes.ESpatialiteGeometryType in project hortonmachine by TheHortonMachine.
the class GeopaparazziSpatialiteCreator method process.
// VARS DOCS END
@Execute
public void process() throws Exception {
checkNull(inGeopaparazzi, inShapefilesFolder);
if (pEncoding == null || pEncoding.trim().length() == 0) {
pEncoding = "UTF-8";
}
if (pSizeFactor < 1) {
pSizeFactor = 3;
}
if (pLinesWidthFactor < 1) {
pLinesWidthFactor = 6;
}
File shpFolder = new File(inShapefilesFolder);
File[] shpfiles = shpFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".shp");
}
});
if (shpfiles.length == 0) {
throw new ModelsIOException("The supplied folder doesn't contain any shapefile.", this);
}
try (ASpatialDb db = new GTSpatialiteThreadsafeDb()) {
if (!db.open(inGeopaparazzi)) {
db.initSpatialMetadata(null);
}
if (!db.hasTable(SqlName.m(GeopaparazziDatabaseProperties.PROPERTIESTABLE))) {
GeopaparazziDatabaseProperties.createPropertiesTable(db);
} else {
QueryResult qres1 = db.getTableRecordsMapFromRawSql("select * from dataproperties", 10);
pm.message("Dataproperties already existing: ");
for (Object[] objs : qres1.data) {
pm.message(Arrays.toString(objs));
}
pm.message("----------------------------------");
}
pm.beginTask("Importing shapefiles...", shpfiles.length);
for (File shpFile : shpfiles) {
SqlName name = SqlName.m(FileUtilities.getNameWithoutExtention(shpFile));
if (db.hasTable(name)) {
pm.errorMessage("Table already existing: " + name);
continue;
}
SimpleFeatureCollection fc = OmsVectorReader.readVector(shpFile.getAbsolutePath());
SimpleFeatureType schema = fc.getSchema();
CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
String epsgStr = CrsUtilities.getCodeFromCrs(crs);
String sirdStr = epsgStr.substring(5);
int srid = Integer.parseInt(sirdStr);
EGeometryType geomType = EGeometryType.forGeometryDescriptor(schema.getGeometryDescriptor());
ESpatialiteGeometryType spatialiteGeometryType = geomType.toSpatialiteGeometryType();
HMImportExportUtils.importShapefileThroughVirtualTable(db, name, shpFile.getAbsolutePath(), pEncoding, srid, spatialiteGeometryType);
Style style = SldUtilities.getStyleFromFile(shpFile);
if (style != null) {
String uniqueName = "/#" + name + "#geometry";
StyleWrapper styleWrapper = new StyleWrapper(style);
List<FeatureTypeStyleWrapper> featureTypeStylesWrapperList = styleWrapper.getFeatureTypeStylesWrapperList();
if (featureTypeStylesWrapperList.size() > 0) {
List<RuleWrapper> rulesWrapperList = new ArrayList<>();
for (FeatureTypeStyleWrapper ftsWrapper : featureTypeStylesWrapperList) {
List<RuleWrapper> rulesWrappers = ftsWrapper.getRulesWrapperList();
rulesWrapperList.addAll(rulesWrappers);
}
if (rulesWrapperList.size() == 1) {
RuleWrapper ruleWrapper = rulesWrapperList.get(0);
SymbolizerWrapper geometrySymbolizersWrapper = ruleWrapper.getGeometrySymbolizersWrapper();
if (geometrySymbolizersWrapper != null) {
org.hortonmachine.dbs.utils.BasicStyle gpStyle = createBaseStyle(db, uniqueName, rulesWrapperList);
populateStyleObject(gpStyle, geometrySymbolizersWrapper);
GeopaparazziDatabaseProperties.updateStyle(db, gpStyle);
}
} else if (rulesWrapperList.size() > 1) {
org.hortonmachine.dbs.utils.BasicStyle gpStyle = createBaseStyle(db, uniqueName, rulesWrapperList);
gpStyle.themeMap = new HashMap<>();
for (RuleWrapper ruleWrapper : rulesWrapperList) {
SymbolizerWrapper geometrySymbolizersWrapper = ruleWrapper.getGeometrySymbolizersWrapper();
org.hortonmachine.dbs.utils.BasicStyle themeStyle = createBaseStyle(null, uniqueName, rulesWrapperList);
populateStyleObject(themeStyle, geometrySymbolizersWrapper);
Filter filter = ruleWrapper.getRule().getFilter();
if (filter instanceof IsEqualsToImpl) {
IsEqualsToImpl equalsFilter = (IsEqualsToImpl) filter;
Expression expression1 = equalsFilter.getExpression1();
Expression expression2 = equalsFilter.getExpression2();
setFilter(gpStyle, themeStyle, expression1);
setFilter(gpStyle, themeStyle, expression2);
}
}
GeopaparazziDatabaseProperties.updateStyle(db, gpStyle);
} else {
pm.errorMessage("Unable to export SLD for: " + shpFile);
continue;
}
}
}
pm.worked(1);
}
pm.done();
QueryResult qres = db.getTableRecordsMapFromRawSql("select * from dataproperties", 100);
pm.message("Dataproperties inserted: ");
int theme = qres.names.indexOf(org.hortonmachine.dbs.utils.BasicStyle.THEME);
for (Object[] objs : qres.data) {
String themeString = objs[theme].toString().replaceAll("\\s+", " ");
if (themeString.length() > 20) {
objs[theme] = themeString.substring(0, 15) + "...";
}
pm.message(Arrays.toString(objs));
}
}
}
use of org.hortonmachine.dbs.datatypes.ESpatialiteGeometryType in project hortonmachine by TheHortonMachine.
the class DatabaseController method addDataTableContextMenu.
private void addDataTableContextMenu(JTable table) {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.setBorder(new BevelBorder(BevelBorder.RAISED));
popupMenu.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
int[] selectedRows = table.getSelectedRows();
int[] selectedCols = table.getSelectedColumns();
boolean isGeom = false;
boolean isBinary = false;
boolean proposeChart = false;
ESpatialiteGeometryType geomType = null;
if (selectedCols.length == 1 && selectedRows.length > 0) {
// check content
Object valueObj = table.getValueAt(selectedRows[0], selectedCols[0]);
if (valueObj instanceof byte[] || valueObj instanceof JdbcBlob) {
isBinary = true;
}
String valueAt = valueObj.toString();
String checkString = valueAt.split("\\(")[0].trim();
checkString = removeSrid(checkString);
if (ESpatialiteGeometryType.isGeometryName(checkString)) {
isGeom = true;
geomType = ESpatialiteGeometryType.forName(checkString);
}
}
if (selectedCols.length > 1 && selectedRows.length > 1) {
proposeChart = true;
}
JMenuItem item = new JMenuItem(new AbstractAction("Copy cells content") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
table.getTransferHandler().exportToClipboard(table, clipboard, TransferHandler.COPY);
}
});
item.setHorizontalTextPosition(JMenuItem.RIGHT);
popupMenu.add(item);
if (isBinary) {
JMenuItem item1 = new JMenuItem(new AbstractAction("View as image") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
for (int r : selectedRows) {
Object valueObj = table.getValueAt(r, selectedCols[0]);
byte[] bytes = null;
if (valueObj instanceof JdbcBlob) {
JdbcBlob blob = (JdbcBlob) valueObj;
try {
bytes = blob.getBytes(0, (int) blob.length());
} catch (SQLException e1) {
Logger.INSTANCE.e("error reading image bytes", e1);
continue;
}
} else if (valueObj instanceof byte[]) {
bytes = (byte[]) valueObj;
}
if (bytes != null) {
try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
GuiUtilities.showImage(popupMenu, valueObj.toString(), image);
} catch (IOException e1) {
GuiUtilities.showWarningMessage(popupMenu, "Not an image.");
break;
}
}
}
}
});
item1.setHorizontalTextPosition(JMenuItem.RIGHT);
popupMenu.add(item1);
JMenuItem itemToString = new JMenuItem(new AbstractAction("View as string") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
for (int r : selectedRows) {
Object valueObj = table.getValueAt(r, selectedCols[0]);
byte[] bytes = null;
if (valueObj instanceof JdbcBlob) {
JdbcBlob blob = (JdbcBlob) valueObj;
try {
bytes = blob.getBytes(0, (int) blob.length());
} catch (SQLException e1) {
Logger.INSTANCE.e("error reading image bytes", e1);
continue;
}
} else if (valueObj instanceof byte[]) {
bytes = (byte[]) valueObj;
}
if (bytes != null) {
String string = new String(bytes);
JTextArea tArea = new JTextArea(string, 10, 20);
JPanel p = new JPanel(new BorderLayout());
final JScrollPane scroll = new JScrollPane(tArea);
p.add(scroll, BorderLayout.CENTER);
tArea.setLineWrap(true);
GuiUtilities.openDialogWithPanel(p, "Cell as string", new Dimension(600, 500), false);
}
}
}
});
itemToString.setHorizontalTextPosition(JMenuItem.RIGHT);
popupMenu.add(itemToString);
}
if (proposeChart) {
JMenuItem item1 = new JMenuItem(new AbstractAction("Chart values") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
try {
int chartsCount = selectedCols.length - 1;
String xLabel = table.getColumnName(selectedCols[0]);
Scatter scatterChart = null;
CategoryHistogram categoryHistogram = null;
for (int i = 0; i < chartsCount; i++) {
Object tmpX = table.getValueAt(0, selectedCols[0]);
boolean doCat = true;
if (tmpX instanceof Number) {
doCat = false;
}
Object tmpY = table.getValueAt(0, selectedCols[i + 1]);
if (!(tmpY instanceof Number)) {
break;
}
if (doCat) {
if (categoryHistogram == null) {
String[] xStr = new String[selectedRows.length];
double[] y = new double[selectedRows.length];
for (int r : selectedRows) {
Object xObj = table.getValueAt(r, selectedCols[0]);
Object yObj = table.getValueAt(r, selectedCols[i + 1]);
xStr[r] = xObj.toString();
y[r] = ((Number) yObj).doubleValue();
}
categoryHistogram = new CategoryHistogram(xStr, y);
}
} else {
if (scatterChart == null) {
scatterChart = new Scatter("");
List<Boolean> showLines = new ArrayList<Boolean>();
for (int j = 0; j < chartsCount; j++) {
showLines.add(true);
}
scatterChart.setShowLines(showLines);
scatterChart.setXLabel(xLabel);
scatterChart.setYLabel("");
}
double[] x = new double[selectedRows.length];
double[] y = new double[selectedRows.length];
String seriesName = table.getColumnName(selectedCols[i + 1]);
for (int r : selectedRows) {
Object xObj = table.getValueAt(r, selectedCols[0]);
Object yObj = table.getValueAt(r, selectedCols[i + 1]);
x[r] = ((Number) xObj).doubleValue();
y[r] = ((Number) yObj).doubleValue();
}
scatterChart.addSeries(seriesName, x, y);
}
}
Dimension dimension = new Dimension(800, 600);
if (scatterChart != null) {
JFreeChart chart = scatterChart.getChart();
ChartPanel chartPanel = new ChartPanel(chart, true);
chartPanel.setPreferredSize(dimension);
JPanel p = new JPanel(new BorderLayout());
p.add(chartPanel, BorderLayout.CENTER);
GuiUtilities.openDialogWithPanel(p, "Chart from cells", dimension, false);
} else if (categoryHistogram != null) {
JFreeChart chart = categoryHistogram.getChart();
ChartPanel chartPanel = new ChartPanel(chart, true);
chartPanel.setPreferredSize(dimension);
JPanel p = new JPanel(new BorderLayout());
p.add(chartPanel, BorderLayout.CENTER);
GuiUtilities.openDialogWithPanel(p, "Chart from cells", dimension, false);
} else {
GuiUtilities.showWarningMessage(popupMenu, "Charting of selected data not possible.");
}
} catch (Exception ex) {
Logger.INSTANCE.insertError("", "ERROR", ex);
}
}
});
item1.setHorizontalTextPosition(JMenuItem.RIGHT);
popupMenu.add(item1);
}
if (isGeom) {
JMenuItem item1 = new JMenuItem(new AbstractAction("View geometry") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
WKTReader wktReader = new WKTReader();
List<Geometry> geomsList = new ArrayList<>();
for (int r : selectedRows) {
try {
String valueAt = table.getValueAt(r, selectedCols[0]).toString();
valueAt = removeSrid(valueAt);
Geometry geometry = wktReader.read(valueAt);
if (geometry instanceof GeometryCollection) {
int numGeometries = geometry.getNumGeometries();
for (int j = 0; j < numGeometries; j++) {
Geometry geometryN = geometry.getGeometryN(j);
geomsList.add(geometryN);
}
} else {
geomsList.add(geometry);
}
} catch (ParseException e1) {
Logger.INSTANCE.insertError("", "ERROR", e1);
}
}
if (geomsList.size() > 0) {
List<SimpleFeatureCollection> fcs = FeatureUtilities.featureCollectionsFromGeometry(null, geomsList.toArray(new Geometry[0]));
showInMapFrame(false, fcs.toArray(new SimpleFeatureCollection[0]), null);
}
}
});
item1.setHorizontalTextPosition(JMenuItem.RIGHT);
popupMenu.add(item1);
JMenuItem item2 = new JMenuItem(new AbstractAction("Plot geometry") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
WKTReader wktReader = new WKTReader();
List<Geometry> geomsList = new ArrayList<>();
for (int r : selectedRows) {
try {
String valueAt = table.getValueAt(r, selectedCols[0]).toString();
valueAt = removeSrid(valueAt);
Geometry geometry = wktReader.read(valueAt);
if (geometry instanceof GeometryCollection) {
int numGeometries = geometry.getNumGeometries();
for (int j = 0; j < numGeometries; j++) {
Geometry geometryN = geometry.getGeometryN(j);
geomsList.add(geometryN);
}
} else {
geomsList.add(geometry);
}
} catch (ParseException e1) {
Logger.INSTANCE.insertError("", "ERROR", e1);
}
}
if (geomsList.size() > 0) {
HM.plotJtsGeometries(null, geomsList);
}
}
});
item2.setHorizontalTextPosition(JMenuItem.RIGHT);
popupMenu.add(item2);
if (geomType != null && !geomType.isPoint()) {
JMenuItem item3 = new JMenuItem(new AbstractAction("View geometry with directions hint") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
WKTReader wktReader = new WKTReader();
List<Geometry> geomsList = new ArrayList<>();
for (int r : selectedRows) {
try {
String valueAt = table.getValueAt(r, selectedCols[0]).toString();
valueAt = removeSrid(valueAt);
Geometry geometry = wktReader.read(valueAt);
if (geometry instanceof GeometryCollection) {
int numGeometries = geometry.getNumGeometries();
for (int j = 0; j < numGeometries; j++) {
Geometry geometryN = geometry.getGeometryN(j);
List<Polygon> simpleDirectionArrows = GeometryUtilities.createSimpleDirectionArrow(geometryN);
geomsList.add(geometryN);
geomsList.addAll(simpleDirectionArrows);
}
} else {
List<Polygon> simpleDirectionArrows = GeometryUtilities.createSimpleDirectionArrow(geometry);
geomsList.add(geometry);
geomsList.addAll(simpleDirectionArrows);
}
} catch (ParseException e1) {
Logger.INSTANCE.insertError("", "ERROR", e1);
}
}
if (geomsList.size() > 0) {
List<SimpleFeatureCollection> fcs = FeatureUtilities.featureCollectionsFromGeometry(null, geomsList.toArray(new Geometry[0]));
showInMapFrame(false, fcs.toArray(new SimpleFeatureCollection[0]), null);
}
}
});
item3.setHorizontalTextPosition(JMenuItem.RIGHT);
popupMenu.add(item3);
}
}
}
private String removeSrid(String valueAt) {
if (valueAt.startsWith("SRID=")) {
valueAt = valueAt.split(";")[1];
}
return valueAt;
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
popupMenu.removeAll();
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
popupMenu.removeAll();
}
});
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
}
Aggregations