use of org.opengis.feature.type.GeometryDescriptor in project sldeditor by robward-scisys.
the class SLDEditorBufferedImageLegendGraphicBuilder method cloneWithDimensionality.
/**
* Clones the given schema, changing the geometry attribute to match the given dimensionality.
*
* @param schema schema to clone
* @param dimensionality dimensionality for the geometry 1= points, 2= lines, 3= polygons
*/
private FeatureType cloneWithDimensionality(FeatureType schema, int dimensionality) {
SimpleFeatureType simpleFt = (SimpleFeatureType) schema;
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName(schema.getName());
builder.setCRS(schema.getCoordinateReferenceSystem());
for (AttributeDescriptor desc : simpleFt.getAttributeDescriptors()) {
if (isMixedGeometry(desc)) {
GeometryDescriptor geomDescriptor = (GeometryDescriptor) desc;
GeometryType geomType = geomDescriptor.getType();
Class<?> geometryClass = getGeometryForDimensionality(dimensionality);
GeometryType gt = new GeometryTypeImpl(geomType.getName(), geometryClass, geomType.getCoordinateReferenceSystem(), geomType.isIdentified(), geomType.isAbstract(), geomType.getRestrictions(), geomType.getSuper(), geomType.getDescription());
builder.add(new GeometryDescriptorImpl(gt, geomDescriptor.getName(), geomDescriptor.getMinOccurs(), geomDescriptor.getMaxOccurs(), geomDescriptor.isNillable(), geomDescriptor.getDefaultValue()));
} else {
builder.add(desc);
}
}
schema = builder.buildFeatureType();
return schema;
}
use of org.opengis.feature.type.GeometryDescriptor in project sldeditor by robward-scisys.
the class DataSourceInfo method getPropertyDescriptorList.
/**
* Gets the property descriptor list.
*
* @return the property descriptor list
*/
public Collection<PropertyDescriptor> getPropertyDescriptorList() {
if (schema != null) {
return schema.getDescriptors();
} else {
if (geometryType == GeometryTypeEnum.RASTER) {
if (rasterPropertyDescriptorList == null) {
rasterPropertyDescriptorList = new ArrayList<>();
CoordinateReferenceSystem crs = null;
boolean isIdentifiable = false;
boolean isAbstract = false;
List<Filter> restrictions = null;
AttributeType superType = null;
InternationalString description = null;
GeometryType type = featureTypeFactory.createGeometryType(new NameImpl(RASTER_GEOMETRY_FIELD), GridCoverage2D.class, crs, isIdentifiable, isAbstract, restrictions, superType, description);
GeometryDescriptor descriptor = featureTypeFactory.createGeometryDescriptor(type, new NameImpl(RASTER_GEOMETRY_FIELD), 0, 1, false, null);
rasterPropertyDescriptorList.add(descriptor);
}
return rasterPropertyDescriptorList;
}
}
return null;
}
use of org.opengis.feature.type.GeometryDescriptor in project sldeditor by robward-scisys.
the class VectorToolTest method testVectorToolFileDataSource.
/**
* Test method for {@link
* com.sldeditor.tool.vector.VectorTool#VectorTool(com.sldeditor.common.SLDEditorInterface)}.
*/
@Test
public void testVectorToolFileDataSource() {
TestMissingSLDAttributes testAttribute = new TestMissingSLDAttributes();
List<CheckAttributeInterface> checkList = new ArrayList<CheckAttributeInterface>();
checkList.add(testAttribute);
CheckAttributeFactory.setOverrideCheckList(checkList);
String testsldfile = "/polygon/sld/polygon_polygonwithdefaultlabel.sld";
TestSLDEditor testSLDEditor = null;
try {
testSLDEditor = TestSLDEditor.createAndShowGUI2(null, null, true, null);
} catch (Exception e) {
e.printStackTrace();
}
InputStream inputStream = VectorToolTest.class.getResourceAsStream(testsldfile);
if (inputStream == null) {
assertNotNull(inputStream, "Failed to find sld test file : " + testsldfile);
} else {
File f = null;
try {
f = stream2file(inputStream);
try {
testSLDEditor.getTestInterface().openFile(f.toURI().toURL());
} catch (NullPointerException nullException) {
nullException.printStackTrace();
StackTraceElement[] stackTraceElements = nullException.getStackTrace();
System.out.println(stackTraceElements[0].getMethodName());
}
f.delete();
} catch (IOException e1) {
e1.printStackTrace();
}
}
// Fields extracted from the SLD file
DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
Collection<PropertyDescriptor> propertyList = dataSource.getPropertyDescriptorList();
assertEquals(2, propertyList.size());
Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
for (PropertyDescriptor property : propertyList) {
map.put(property.getName().getLocalPart(), property);
}
AttributeDescriptor name = (AttributeDescriptor) map.get("name");
assertNotNull(name);
GeometryDescriptor geometry = (GeometryDescriptor) map.get("geom");
assertNotNull(geometry);
Path tempFolder = null;
try {
tempFolder = Files.createTempDirectory(getClass().getSimpleName());
} catch (IOException e) {
e.printStackTrace();
fail("Failed to create temp folder in temp folder!");
}
TestVectorTool vectorTool = new TestVectorTool(new SLDEditorMain(new JPanel()));
try {
// Set a shape file as a data source - that matches the SLD
File matchingShpFile = extractShapeFile(tempFolder.toFile(), "/test/sld_cookbook_polygon.zip");
FileTreeNode fileTreeNode = new FileTreeNode(matchingShpFile.getParentFile(), matchingShpFile.getName());
vectorTool.testSetDataSource(fileTreeNode);
dataSource = DataSourceFactory.createDataSource(null);
propertyList = dataSource.getPropertyDescriptorList();
assertEquals(3, propertyList.size());
map.clear();
for (PropertyDescriptor property : propertyList) {
map.put(property.getName().getLocalPart(), property);
}
name = (AttributeDescriptor) map.get("name");
assertNotNull(name);
geometry = (GeometryDescriptor) map.get("the_geom");
assertNotNull(geometry);
AttributeDescriptor pop = (AttributeDescriptor) map.get("pop");
assertNotNull(pop);
// Set a shape file as a data source - that does not match the SLD
File nonMatchingShpFile = extractShapeFile(tempFolder.toFile(), "/test/states.zip");
FileTreeNode fileTreeNode2 = new FileTreeNode(nonMatchingShpFile.getParentFile(), nonMatchingShpFile.getName());
vectorTool.testSetDataSource(fileTreeNode2);
dataSource = DataSourceFactory.createDataSource(null);
propertyList = dataSource.getPropertyDescriptorList();
assertEquals(23, propertyList.size());
map.clear();
for (PropertyDescriptor property : propertyList) {
map.put(property.getName().getLocalPart(), property);
}
name = (AttributeDescriptor) map.get("name");
assertNull(name);
geometry = (GeometryDescriptor) map.get("the_geom");
assertNotNull(geometry);
pop = (AttributeDescriptor) map.get("pop");
assertNull(pop);
assertEquals(1, testAttribute.getMissingFieldList().size());
assertEquals("name", testAttribute.getMissingFieldList().get(0));
// Create SLD from shape file
vectorTool.testImportFile(fileTreeNode);
dataSource = DataSourceFactory.createDataSource(null);
propertyList = dataSource.getPropertyDescriptorList();
assertEquals(3, propertyList.size());
map.clear();
for (PropertyDescriptor property : propertyList) {
map.put(property.getName().getLocalPart(), property);
}
name = (AttributeDescriptor) map.get("name");
assertNotNull(name);
geometry = (GeometryDescriptor) map.get("the_geom");
assertNotNull(geometry);
pop = (AttributeDescriptor) map.get("pop");
assertNotNull(pop);
// Release locks
dataSource.reset();
} catch (IOException e) {
fail(e.getStackTrace().toString());
}
// Tidy up so the remaining unit tests are ok
JFrame frame = Controller.getInstance().getFrame();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
testSLDEditor = null;
clearDown();
// Delete the shape files we extracted
purgeDirectory(tempFolder);
}
use of org.opengis.feature.type.GeometryDescriptor in project sldeditor by robward-scisys.
the class VectorToolTest method testVectorToolDBDataSource.
@Test
public void testVectorToolDBDataSource() {
TestMissingSLDAttributes testAttribute = new TestMissingSLDAttributes();
List<CheckAttributeInterface> checkList = new ArrayList<CheckAttributeInterface>();
checkList.add(testAttribute);
CheckAttributeFactory.setOverrideCheckList(checkList);
String testsldfile = "/polygon/sld/polygon_polygonwithdefaultlabel.sld";
TestSLDEditor testSLDEditor = null;
try {
testSLDEditor = TestSLDEditor.createAndShowGUI2(null, null, true, null);
} catch (Exception e) {
e.printStackTrace();
}
RenderPanelImpl.setUnderTest(true);
InputStream inputStream = VectorToolTest.class.getResourceAsStream(testsldfile);
if (inputStream == null) {
assertNotNull(inputStream, "Failed to find sld test file : " + testsldfile);
} else {
File f = null;
try {
f = stream2file(inputStream);
try {
testSLDEditor.getTestInterface().openFile(f.toURI().toURL());
} catch (NullPointerException nullException) {
nullException.printStackTrace();
StackTraceElement[] stackTraceElements = nullException.getStackTrace();
System.out.println(stackTraceElements[0].getMethodName());
}
f.delete();
} catch (IOException e1) {
e1.printStackTrace();
}
}
// Fields extracted from the SLD file
DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
Collection<PropertyDescriptor> propertyList = dataSource.getPropertyDescriptorList();
assertEquals(2, propertyList.size());
Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
for (PropertyDescriptor property : propertyList) {
map.put(property.getName().getLocalPart(), property);
}
AttributeDescriptor name = (AttributeDescriptor) map.get("name");
assertNotNull(name);
GeometryDescriptor geometry = (GeometryDescriptor) map.get("geom");
assertNotNull(geometry);
Path tempFolder = null;
try {
tempFolder = Files.createTempDirectory(getClass().getSimpleName());
} catch (IOException e) {
e.printStackTrace();
fail("Failed to create temp folder in temp folder!");
}
TestVectorTool vectorTool = new TestVectorTool(new SLDEditorMain(new JPanel()));
try {
InputStream gpkgInputStream = VectorToolTest.class.getResourceAsStream("/test/sld_cookbook_polygon.gpkg");
final File gpkgFile = new File(tempFolder.toFile(), "sld_cookbook_polygon.gpkg");
try (FileOutputStream out = new FileOutputStream(gpkgFile)) {
IOUtils.copy(gpkgInputStream, out);
}
DatabaseConnection databaseConnection = DatabaseConnectionFactory.getConnection(gpkgFile.getAbsolutePath());
DatabaseFeatureClassNode dbFCTreeNode = new DatabaseFeatureClassNode(null, databaseConnection, "sld_cookbook_polygon");
DatabaseConnectionManager.getInstance().addNewConnection(null, databaseConnection);
vectorTool.testSetDataSource(dbFCTreeNode);
dataSource = DataSourceFactory.createDataSource(null);
propertyList = dataSource.getPropertyDescriptorList();
assertEquals(3, propertyList.size());
map.clear();
for (PropertyDescriptor property : propertyList) {
map.put(property.getName().getLocalPart(), property);
}
name = (AttributeDescriptor) map.get("name");
assertNotNull(name);
geometry = (GeometryDescriptor) map.get("geometry");
assertNotNull(geometry);
AttributeDescriptor pop = (AttributeDescriptor) map.get("pop");
assertNotNull(pop);
// Create SLD from geopackage layer
vectorTool.testImportFeatureClass(dbFCTreeNode);
dataSource = DataSourceFactory.createDataSource(null);
propertyList = dataSource.getPropertyDescriptorList();
assertEquals(3, propertyList.size());
map.clear();
for (PropertyDescriptor property : propertyList) {
map.put(property.getName().getLocalPart(), property);
}
name = (AttributeDescriptor) map.get("name");
assertNotNull(name);
geometry = (GeometryDescriptor) map.get("geometry");
assertNotNull(geometry);
pop = (AttributeDescriptor) map.get("pop");
assertNotNull(pop);
// Release locks
dataSource.reset();
} catch (IOException e) {
fail(e.getStackTrace().toString());
}
// Tidy up so the remaining unit tests are ok
JFrame frame = Controller.getInstance().getFrame();
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
testSLDEditor = null;
clearDown();
// Delete the shape files we extracted
purgeDirectory(tempFolder);
}
Aggregations