use of org.geotools.data.store.ReprojectingFeatureCollection in project georchestra by georchestra.
the class KmlFeatureSource method getFeatures.
/**
* Reads the kml file
* <p>
* Note: only Filter.INCLUDE is implemented
* </p>
*
* @param query
* @return {@link ListFeatureCollection }
*/
public SimpleFeatureCollection getFeatures(Query query) throws IOException {
SimpleFeatureCollection collection = toFeatureCollection();
CoordinateReferenceSystem sourceCrs = collection.getSchema().getCoordinateReferenceSystem();
CoordinateReferenceSystem targetCRS = query.getCoordinateSystemReproject();
if (targetCRS != null && !CRS.equalsIgnoreMetadata(sourceCrs, targetCRS)) {
collection = new ReprojectingFeatureCollection(collection, targetCRS);
}
// Remove the Style property
SimpleFeatureType schema = collection.getSchema();
if (null != schema.getDescriptor("Style")) {
String[] properties = schema.getDescriptors().stream().map(PropertyDescriptor::getName).map(Name::getLocalPart).filter(name -> !"Style".equals(name)).collect(Collectors.toList()).toArray(new String[0]);
SimpleFeatureType targetType;
try {
targetType = DataUtilities.createSubType(schema, properties);
} catch (SchemaException e) {
throw new IOException(e);
}
collection = new ReTypingFeatureCollection(collection, targetType);
}
return collection;
}
use of org.geotools.data.store.ReprojectingFeatureCollection in project hortonmachine by TheHortonMachine.
the class NwwUtilities method reprojectToWGS84.
private static SimpleFeatureCollection reprojectToWGS84(SimpleFeatureCollection fc) {
// BOUNDS
ReferencedEnvelope bounds = fc.getBounds();
CoordinateReferenceSystem crs = bounds.getCoordinateReferenceSystem();
if (!CRS.equalsIgnoreMetadata(crs, GPS_CRS)) {
try {
fc = new ReprojectingFeatureCollection(fc, GPS_CRS);
} catch (Exception e) {
throw new IllegalArgumentException("The data need to be of WGS84 lat/lon projection.", e);
}
}
return fc;
}
use of org.geotools.data.store.ReprojectingFeatureCollection in project hortonmachine by TheHortonMachine.
the class MainController method createMenuActions.
private void createMenuActions(JPopupMenu popupMenu) {
boolean isRaster = false;
if (currentLayer instanceof RasterLayer) {
isRaster = true;
}
if (currentSelectedSW != null && !isRaster) {
// add featureStyle
AbstractAction action = new AbstractAction("Add new FeatureTypeStyle") {
@Override
public void actionPerformed(ActionEvent e) {
FeatureTypeStyle featureTypeStyle = StyleUtilities.sf.createFeatureTypeStyle();
FeatureTypeStyleWrapper ftsw = new FeatureTypeStyleWrapper(featureTypeStyle, currentSelectedSW);
String tmpName = "New Group";
tmpName = WrapperUtilities.checkSameNameFeatureTypeStyle(styleWrapper.getFeatureTypeStylesWrapperList(), tmpName);
ftsw.setName(tmpName);
styleWrapper.addFeatureTypeStyle(ftsw);
reloadGroupsAndRules();
}
};
JMenuItem item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
} else if (currentSelectedFSW != null && !isRaster) {
// add rule
AbstractAction action = new AbstractAction("Add new Rule") {
@Override
public void actionPerformed(ActionEvent e) {
Rule rule = StyleUtilities.sf.createRule();
RuleWrapper rw = new RuleWrapper(rule, currentSelectedFSW);
currentSelectedFSW.addRule(rw);
String tmpName = "New Rule";
tmpName = WrapperUtilities.checkSameNameRule(currentSelectedFSW.getRulesWrapperList(), tmpName);
rw.setName(tmpName);
reloadGroupsAndRules();
}
};
JMenuItem item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
action = new AbstractAction("Remove all Rules") {
@Override
public void actionPerformed(ActionEvent e) {
currentSelectedFSW.clear();
reloadGroupsAndRules();
}
};
item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
List<FeatureTypeStyleWrapper> featureTypeStylesWrapperList = styleWrapper.getFeatureTypeStylesWrapperList();
int ftIndex = featureTypeStylesWrapperList.indexOf(currentSelectedFSW);
int ftSize = featureTypeStylesWrapperList.size();
if (ftSize > 1 && ftIndex > 0) {
action = new AbstractAction("Move up") {
@Override
public void actionPerformed(ActionEvent e) {
int from = ftIndex;
int to = ftIndex - 1;
if (to >= 0) {
styleWrapper.swap(from, to);
reloadGroupsAndRules();
}
}
};
item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
}
popupMenu.add(new JSeparator());
// remove featureStyle
action = new AbstractAction("Remove selected FeatureTypeStyle") {
@Override
public void actionPerformed(ActionEvent e) {
StyleWrapper p = currentSelectedFSW.getParent();
p.removeFeatureTypeStyle(currentSelectedFSW);
reloadGroupsAndRules();
}
};
item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
} else if (currentSelectedRW != null && !isRaster) {
// add rule
AbstractAction action;
JMenuItem item;
if (currentSelectedRW.getGeometrySymbolizersWrapper() == null) {
action = new AbstractAction("Add Geometry Symbolizer") {
@Override
public void actionPerformed(ActionEvent e) {
EGeometryType type = EGeometryType.forGeometryDescriptor(geometryDescriptor);
switch(type) {
case POINT:
case MULTIPOINT:
currentSelectedRW.addSymbolizer(null, PointSymbolizerWrapper.class);
break;
case LINESTRING:
case MULTILINESTRING:
currentSelectedRW.addSymbolizer(null, LineSymbolizerWrapper.class);
break;
case POLYGON:
case MULTIPOLYGON:
currentSelectedRW.addSymbolizer(null, PolygonSymbolizerWrapper.class);
break;
default:
break;
}
reloadGroupsAndRules();
}
};
item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
}
if (currentSelectedRW.getTextSymbolizersWrapper() == null && currentSelectedRW.getGeometrySymbolizersWrapper() != null) {
action = new AbstractAction("Add Text Symbolizer") {
@Override
public void actionPerformed(ActionEvent e) {
currentSelectedRW.addSymbolizer(null, TextSymbolizerWrapper.class);
reloadGroupsAndRules();
}
};
item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
}
popupMenu.add(new JSeparator());
// remove featureStyle
action = new AbstractAction("Remove selected Rule") {
@Override
public void actionPerformed(ActionEvent e) {
FeatureTypeStyleWrapper f = currentSelectedRW.getParent();
f.removeRule(currentSelectedRW);
reloadGroupsAndRules();
}
};
item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
} else if (currentSelectedSymW != null && !isRaster) {
// remove featureStyle
AbstractAction action = new AbstractAction("Remove selected Symbolizer") {
@Override
public void actionPerformed(ActionEvent e) {
RuleWrapper rw = currentSelectedSymW.getParent();
rw.removeSymbolizerWrapper(currentSelectedSymW);
reloadGroupsAndRules();
}
};
JMenuItem item = new JMenuItem(action);
popupMenu.add(item);
item.setHorizontalTextPosition(JMenuItem.RIGHT);
} else if (currentSelectedFeatureAttributeNode != null && !isRaster) {
AbstractAction action1 = new AbstractAction("View field stats") {
@Override
public void actionPerformed(ActionEvent e) {
String fieldName = currentSelectedFeatureAttributeNode.getFieldName();
String geomName = geometryDescriptor.getLocalName();
String[][] dataMatrix = null;
if (geomName.equals(fieldName)) {
int count = 0;
double length = 0;
double area = 0;
for (SimpleFeature simpleFeature : currentFeaturesList) {
Geometry geom = (Geometry) simpleFeature.getDefaultGeometry();
length += geom.getLength();
area += geom.getArea();
count++;
}
SimpleFeatureCollection fl3857FC = new ReprojectingFeatureCollection(currentFeatureCollection, CrsUtilities.getCrsFromSrid(3857));
List<SimpleFeature> fl3857List = FeatureUtilities.featureCollectionToList(fl3857FC);
double length3857 = 0;
double area3857 = 0;
for (SimpleFeature simpleFeature : fl3857List) {
Geometry geom = (Geometry) simpleFeature.getDefaultGeometry();
length3857 += geom.getLength();
area3857 += geom.getArea();
}
DecimalFormat f = new DecimalFormat("0.0");
DecimalFormat fd = new DecimalFormat("0.000000");
dataMatrix = new String[5][2];
dataMatrix[0][0] = "Features count";
dataMatrix[0][1] = "" + count;
dataMatrix[1][0] = "Length (data CRS/units)";
dataMatrix[1][1] = fd.format(length);
dataMatrix[2][0] = "Area (data CRS/units)";
dataMatrix[2][1] = fd.format(area);
dataMatrix[3][0] = "Length (3857 CRS)";
dataMatrix[3][1] = f.format(length3857) + "m";
dataMatrix[4][0] = "Area (3857)";
dataMatrix[4][1] = f.format(area3857) + "m";
Dimension dimension = new Dimension(400, 200);
boolean modal = true;
String title = "Geometry stats";
String[] columnNames = new String[] { "", "" };
GuiUtilities.openDialogWithTable(title, dataMatrix, columnNames, dimension, modal);
} else {
TreeMap<String, Integer> map = new TreeMap<>();
for (SimpleFeature simpleFeature : currentFeaturesList) {
Object attribute = simpleFeature.getAttribute(fieldName);
if (attribute != null) {
String attrStr = attribute.toString();
Integer count = map.get(attrStr);
if (count == null) {
count = 1;
} else {
count = count + 1;
}
map.put(attrStr, count);
}
}
int row = 0;
dataMatrix = new String[map.size()][2];
for (Entry<String, Integer> entry : map.entrySet()) {
String name = entry.getKey();
String count = String.valueOf(entry.getValue());
dataMatrix[row][0] = name;
dataMatrix[row][1] = count;
row++;
}
Dimension dimension = new Dimension(400, 600);
boolean modal = true;
String title = "Field stats";
String[] columnNames = new String[] { "value", "count" };
GuiUtilities.openDialogWithTable(title, dataMatrix, columnNames, dimension, modal);
}
}
};
JMenuItem item1 = new JMenuItem(action1);
popupMenu.add(item1);
item1.setHorizontalTextPosition(JMenuItem.RIGHT);
AbstractAction action2 = new AbstractAction("Create unique rules based on this attribute") {
@Override
public void actionPerformed(ActionEvent e) {
FeatureTypeStyleWrapper fsw = currentSelectedFSW;
if (fsw == null) {
// use the first available
List<FeatureTypeStyleWrapper> featureTypeStylesWrapperList = styleWrapper.getFeatureTypeStylesWrapperList();
fsw = featureTypeStylesWrapperList.get(0);
}
String fieldName = currentSelectedFeatureAttributeNode.getFieldName();
TreeSet<String> set = new TreeSet<>();
boolean isStringAttr = false;
for (SimpleFeature simpleFeature : currentFeaturesList) {
Object attribute = simpleFeature.getAttribute(fieldName);
if (attribute != null) {
String attrStr = attribute.toString();
set.add(attrStr);
}
if (attribute instanceof String) {
isStringAttr = true;
}
}
int fillAlpha = 100;
float fillAlphaf = fillAlpha / 255f;
ColorInterpolator interpFill = new ColorInterpolator(EColorTables.rainbow.name(), 0, set.size(), null);
ColorInterpolator interpStroke = new ColorInterpolator(EColorTables.rainbow.name(), 0, set.size(), null);
EGeometryType type = EGeometryType.forGeometryDescriptor(geometryDescriptor);
// create new rules for each attribute value
int index = 0;
for (String value : set) {
try {
Rule rule = StyleUtilities.sf.createRule();
RuleWrapper rw = new RuleWrapper(rule, fsw);
fsw.addRule(rw);
String tmpName = fieldName + " == " + value;
tmpName = WrapperUtilities.checkSameNameRule(fsw.getRulesWrapperList(), tmpName);
rw.setName(tmpName);
String escapedValue = value.replaceAll("'", "''");
String filterValue = escapedValue;
if (isStringAttr) {
filterValue = "'" + escapedValue + "'";
}
Filter filter = FilterUtilities.getCQLFilter(fieldName + "=" + filterValue);
rule.setFilter(filter);
Color fill = interpFill.getColorFor(index);
Color stroke = interpStroke.getColorFor(index);
String fillHex = ColorUtilities.asHex(fill);
String strokeHex = ColorUtilities.asHex(stroke);
switch(type) {
case POINT:
case MULTIPOINT:
PointSymbolizerWrapper pointSW = rw.addSymbolizer(null, PointSymbolizerWrapper.class);
pointSW.setFillColor(fillHex);
pointSW.setFillOpacity(fillAlphaf + "", false);
pointSW.setStrokeColor(strokeHex);
break;
case LINESTRING:
case MULTILINESTRING:
LineSymbolizerWrapper lineSW = rw.addSymbolizer(null, LineSymbolizerWrapper.class);
lineSW.setStrokeColor(strokeHex, false);
break;
case POLYGON:
case MULTIPOLYGON:
PolygonSymbolizerWrapper polygonSW = rw.addSymbolizer(null, PolygonSymbolizerWrapper.class);
polygonSW.setFillColor(fillHex, false);
polygonSW.setFillOpacity(fillAlphaf + "", false);
polygonSW.setStrokeColor(strokeHex, false);
break;
default:
break;
}
index++;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
reloadGroupsAndRules();
applyStyle();
}
};
JMenuItem item2 = new JMenuItem(action2);
popupMenu.add(item2);
item2.setHorizontalTextPosition(JMenuItem.RIGHT);
}
}
use of org.geotools.data.store.ReprojectingFeatureCollection in project hortonmachine by TheHortonMachine.
the class DatabaseViewer method viewSpatialQueryResult.
public boolean viewSpatialQueryResult(String tableName, String sqlText, IHMProgressMonitor pm, boolean isTableQuery) {
boolean hasError = false;
if (sqlText.trim().length() == 0) {
return false;
}
try {
pm.beginTask("Run query: " + sqlText, IHMProgressMonitor.UNKNOWN);
DefaultFeatureCollection fc = DbsHelper.runRawSqlToFeatureCollection(tableName, (ASpatialDb) currentConnectedSqlDatabase, sqlText, null);
ReprojectingFeatureCollection rfc = new ReprojectingFeatureCollection(fc, NwwUtilities.GPS_CRS);
Style[] styles = null;
if (isTableQuery && tableName != null) {
// try to get some style
if (currentConnectedSqlDatabase.getType() == EDb.GEOPACKAGE) {
String sldString = ((GeopackageCommonDb) currentConnectedSqlDatabase).getSldString(SqlName.m(tableName));
if (sldString != null) {
Style style = SldUtilities.getStyleFromSldString(sldString);
styles = new Style[] { style };
}
}
}
showInMapFrame(true, new SimpleFeatureCollection[] { rfc }, styles);
addQueryToHistoryCombo(sqlText);
} catch (Exception e1) {
String localizedMessage = e1.getLocalizedMessage();
hasError = true;
pm.errorMessage("An error occurred: " + localizedMessage);
} finally {
pm.done();
}
return hasError;
}
use of org.geotools.data.store.ReprojectingFeatureCollection in project hortonmachine by TheHortonMachine.
the class DatabaseViewer method viewSpatialQueryResult3D.
public boolean viewSpatialQueryResult3D(String title, String sqlText, IHMProgressMonitor pm) {
boolean hasError = false;
if (sqlText.trim().length() == 0) {
return false;
}
try {
if (toolsPanelController == null) {
openNww();
}
pm.beginTask("Run query: " + sqlText, IHMProgressMonitor.UNKNOWN);
DefaultFeatureCollection fc = DbsHelper.runRawSqlToFeatureCollection(title, (ASpatialDb) currentConnectedSqlDatabase, sqlText, null);
ReprojectingFeatureCollection rfc = new ReprojectingFeatureCollection(fc, NwwUtilities.GPS_CRS);
if (toolsPanelController != null) {
if (title == null) {
title = "QueryLayer";
}
toolsPanelController.loadFeatureCollection(null, title, null, rfc, null);
addQueryToHistoryCombo(sqlText);
}
} catch (Exception e1) {
String localizedMessage = e1.getLocalizedMessage();
hasError = true;
e1.printStackTrace();
pm.errorMessage("An error occurred: " + localizedMessage);
} finally {
pm.done();
}
return hasError;
}
Aggregations