Search in sources :

Example 21 with DataSourceInterface

use of com.sldeditor.datasource.DataSourceInterface in project sldeditor by robward-scisys.

the class VectorTool method setDataSource.

/**
 * Sets the data source.
 *
 * @param fileTreeNode the new data source
 */
protected void setDataSource(FileTreeNode fileTreeNode) {
    File vectorFile = fileTreeNode.getFile();
    ConsoleManager.getInstance().information(this, String.format("%s : %s", Localisation.getString(VectorTool.class, "VectorTool.setDataSource"), vectorFile.getAbsolutePath()));
    // Vector file
    DataSourceConnectorInterface dsc = DataSourceConnectorFactory.getDataSource(DataSourceConnector.class);
    DataSourcePropertiesInterface dsProperties = null;
    try {
        dsProperties = dsc.getDataSourceProperties(DataSourceProperties.encodeFilename(vectorFile.toURI().toURL().toString()));
    } catch (MalformedURLException exceptionObj) {
        ConsoleManager.getInstance().exception(VectorTool.class, exceptionObj);
        return;
    }
    SLDEditorFile sldEditorFile = SLDEditorFile.getInstance();
    sldEditorFile.setDataSource(dsProperties);
    DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
    if (dataSource != null) {
        String dataSourceName = ExternalFilenames.removeSuffix(vectorFile.getName());
        dataSource.connect(dataSourceName, sldEditorFile, CheckAttributeFactory.getCheckList());
    }
}
Also used : DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) MalformedURLException(java.net.MalformedURLException) DataSourceConnectorInterface(com.sldeditor.common.DataSourceConnectorInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File)

Example 22 with DataSourceInterface

use of com.sldeditor.datasource.DataSourceInterface in project sldeditor by robward-scisys.

the class DataSourceFactoryTest method testCreateDataSource.

/**
 * Test method for
 * {@link com.sldeditor.datasource.impl.DataSourceFactory#createDataSource(java.lang.String)}.
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceFactory#getDataSource()}.
 */
@Test
public void testCreateDataSource() {
    DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
    assertTrue(dataSource != null);
    assertTrue(DataSourceFactory.getDataSource() != null);
}
Also used : DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) CreateDataSourceInterface(com.sldeditor.datasource.impl.CreateDataSourceInterface) Test(org.junit.Test)

Example 23 with DataSourceInterface

use of com.sldeditor.datasource.DataSourceInterface 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.setOverideCheckList(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) {
        Assert.assertNotNull("Failed to find sld test file : " + testsldfile, inputStream);
    } else {
        File f = null;
        try {
            f = stream2file(inputStream);
            try {
                testSLDEditor.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);
    File tempFolder = Files.createTempDir();
    TestVectorTool vectorTool = new TestVectorTool(testSLDEditor);
    try {
        InputStream gpkgInputStream = VectorToolTest.class.getResourceAsStream("/test/sld_cookbook_polygon.gpkg");
        final File gpkgFile = new File(tempFolder, "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) {
        e.printStackTrace();
        fail();
    }
    // Tidy up so the remaining unit tests are ok
    JFrame frame = testSLDEditor.getApplicationFrame();
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    testSLDEditor = null;
    clearDown();
    // Delete the shape files we extracted
    purgeDirectory(tempFolder);
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) DatabaseFeatureClassNode(com.sldeditor.datasource.extension.filesystem.node.database.DatabaseFeatureClassNode) JFrame(javax.swing.JFrame) FileOutputStream(java.io.FileOutputStream) WindowEvent(java.awt.event.WindowEvent) DatabaseConnection(com.sldeditor.common.data.DatabaseConnection) CheckAttributeInterface(com.sldeditor.datasource.checks.CheckAttributeInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File) Test(org.junit.Test)

Example 24 with DataSourceInterface

use of com.sldeditor.datasource.DataSourceInterface 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.setOverideCheckList(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) {
        Assert.assertNotNull("Failed to find sld test file : " + testsldfile, inputStream);
    } else {
        File f = null;
        try {
            f = stream2file(inputStream);
            try {
                testSLDEditor.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);
    File tempFolder = Files.createTempDir();
    TestVectorTool vectorTool = new TestVectorTool(testSLDEditor);
    try {
        // Set a shape file as a data source - that matches the SLD
        File matchingShpFile = extractShapeFile(tempFolder, "/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, "/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) {
        e.printStackTrace();
        fail();
    }
    // Tidy up so the remaining unit tests are ok
    JFrame frame = testSLDEditor.getApplicationFrame();
    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
    testSLDEditor = null;
    clearDown();
    // Delete the shape files we extracted
    purgeDirectory(tempFolder);
}
Also used : PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileTreeNode(com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode) DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) GeometryDescriptor(org.opengis.feature.type.GeometryDescriptor) JFrame(javax.swing.JFrame) WindowEvent(java.awt.event.WindowEvent) CheckAttributeInterface(com.sldeditor.datasource.checks.CheckAttributeInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File) Test(org.junit.Test)

Aggregations

DataSourceInterface (com.sldeditor.datasource.DataSourceInterface)24 Test (org.junit.Test)7 CreateDataSourceInterface (com.sldeditor.datasource.impl.CreateDataSourceInterface)6 ArrayList (java.util.ArrayList)6 FieldConfigCommonData (com.sldeditor.ui.detail.config.FieldConfigCommonData)5 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)4 File (java.io.File)4 DataSourceConnectorInterface (com.sldeditor.common.DataSourceConnectorInterface)3 DataSourcePropertiesInterface (com.sldeditor.common.DataSourcePropertiesInterface)3 FieldConfigGeometryField (com.sldeditor.ui.detail.config.FieldConfigGeometryField)3 HashMap (java.util.HashMap)3 UndoEvent (com.sldeditor.common.undo.UndoEvent)2 CheckAttributeInterface (com.sldeditor.datasource.checks.CheckAttributeInterface)2 FileTreeNode (com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode)2 FieldConfigDSProperties (com.sldeditor.ui.detail.config.FieldConfigDSProperties)2 WindowEvent (java.awt.event.WindowEvent)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2