Search in sources :

Example 1 with ChooseRasterFormatInterface

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

the class DetermineRasterFormatTest method testChoose.

/**
 * Test method for
 * {@link com.sldeditor.datasource.chooseraster.DetermineRasterFormat#choose(java.io.File, com.sldeditor.datasource.chooseraster.ChooseRasterFormatInterface)}.
 */
@Test
public void testChoose() {
    AbstractGridFormat gridFormat = DetermineRasterFormat.choose(null, null);
    assertTrue(UnknownFormat.class == gridFormat.getClass());
    URL url = SLDTreeTest.class.getResource("/raster/sld/sld_cookbook_raster.tif");
    File f = new File(url.getFile());
    gridFormat = DetermineRasterFormat.choose(f, null);
    assertTrue(gridFormat != null);
    // Force to WorldImageFormat
    gridFormat = DetermineRasterFormat.choose(f, new ChooseRasterFormatInterface() {

        @Override
        public AbstractGridFormat showPanel(Set<AbstractGridFormat> formatList) {
            WorldImageFormat wif = new WorldImageFormat();
            return wif;
        }
    });
    assertTrue(WorldImageFormat.class == gridFormat.getClass());
}
Also used : WorldImageFormat(org.geotools.gce.image.WorldImageFormat) Set(java.util.Set) UnknownFormat(org.geotools.coverage.grid.io.UnknownFormat) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) ChooseRasterFormatInterface(com.sldeditor.datasource.chooseraster.ChooseRasterFormatInterface) File(java.io.File) URL(java.net.URL) SLDTreeTest(com.sldeditor.test.unit.ui.tree.SLDTreeTest) Test(org.junit.Test)

Example 2 with ChooseRasterFormatInterface

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

the class CreateExternalDataSource method connect.

/**
 * Connect.
 *
 * @param typeName the type name
 * @param geometryFieldName the geometry field name
 * @param editorFile the editor file
 * @return the list of datastores
 */
@Override
public List<DataSourceInfo> connect(String typeName, String geometryFieldName, SLDEditorFileInterface editorFile) {
    List<DataSourceInfo> dataSourceInfoList = new ArrayList<DataSourceInfo>();
    dataSourceInfoList.add(dsInfo);
    dsInfo.reset();
    if (editorFile != null) {
        SLDDataInterface sldData = editorFile.getSLDData();
        DataSourcePropertiesInterface dataSourceProperties = sldData.getDataSourceProperties();
        Map<String, Object> map = dataSourceProperties.getConnectionProperties();
        if (dataSourceProperties.hasPassword()) {
            String password = dataSourceProperties.getPassword();
            if (password == null) {
                password = "dummy password";
                dataSourceProperties.setPassword(password);
                map = dataSourceProperties.getConnectionProperties();
            }
        }
        DataStore dataStore = null;
        try {
            dataStore = DataStoreFinder.getDataStore(map);
            if (dataStore != null) {
                // Try connecting to a vector data source
                dsInfo.setTypeName(typeName);
                SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
                SimpleFeatureType schema = source.getSchema();
                if (schema.getCoordinateReferenceSystem() == null) {
                    // No crs found to set a default and reload
                    if (dataStore instanceof ShapefileDataStore) {
                        ShapefileDataStore shapeFileDatastore = (ShapefileDataStore) dataStore;
                        CoordinateReferenceSystem crs = JCRSChooser.showDialog(Localisation.getString(CreateExternalDataSource.class, "CRSPanel.title"), defaultCRS.getIdentifiers().iterator().next().toString());
                        if (crs != null) {
                            shapeFileDatastore.forceSchemaCRS(crs);
                        }
                        source = dataStore.getFeatureSource(typeName);
                        schema = source.getSchema();
                    }
                }
                dsInfo.setSchema(schema);
                determineGeometryType(schema.getGeometryDescriptor().getType());
            } else {
                // Try connecting to a raster data source
                Object rasterFilename = map.get(DataSourceConnectorInterface.FILE_MAP_KEY);
                if (rasterFilename != null) {
                    File rasterFile = new File(ExternalFilenames.convertURLToFile((String) rasterFilename));
                    ChooseRasterFormatInterface panel = new ChooseRasterFormatPanel(Controller.getInstance().getFrame());
                    AbstractGridFormat format = DetermineRasterFormat.choose(rasterFile, panel);
                    AbstractGridCoverage2DReader reader = format.getReader(rasterFile);
                    dsInfo.setGridCoverageReader(reader);
                } else {
                    logger.error("No matching datastore");
                }
            }
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
        dsInfo.setDataStore(dataStore);
        if (!dsInfo.hasData()) {
            ConsoleManager.getInstance().error(this, Localisation.getField(CreateExternalDataSource.class, "CreateExternalDataSource.failedToConnect") + dataSourceProperties.getDebugConnectionString());
        }
    }
    return dataSourceInfoList;
}
Also used : ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) IOException(java.io.IOException) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SLDDataInterface(com.sldeditor.common.SLDDataInterface) ChooseRasterFormatPanel(com.sldeditor.datasource.chooseraster.ChooseRasterFormatPanel) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) DataStore(org.geotools.data.DataStore) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) ChooseRasterFormatInterface(com.sldeditor.datasource.chooseraster.ChooseRasterFormatInterface) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) File(java.io.File)

Aggregations

ChooseRasterFormatInterface (com.sldeditor.datasource.chooseraster.ChooseRasterFormatInterface)2 File (java.io.File)2 AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)2 DataSourcePropertiesInterface (com.sldeditor.common.DataSourcePropertiesInterface)1 SLDDataInterface (com.sldeditor.common.SLDDataInterface)1 ChooseRasterFormatPanel (com.sldeditor.datasource.chooseraster.ChooseRasterFormatPanel)1 SLDTreeTest (com.sldeditor.test.unit.ui.tree.SLDTreeTest)1 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 AbstractGridCoverage2DReader (org.geotools.coverage.grid.io.AbstractGridCoverage2DReader)1 UnknownFormat (org.geotools.coverage.grid.io.UnknownFormat)1 DataStore (org.geotools.data.DataStore)1 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 WorldImageFormat (org.geotools.gce.image.WorldImageFormat)1 Test (org.junit.Test)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1