Revision 419

View differences:

tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/java/org/gvsig/gdal/prov/gml/GMLLibrary.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.gml;
25

  
26
import org.gvsig.fmap.dal.DALLocator;
27
import org.gvsig.fmap.dal.FileHelper;
28
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
29
import org.gvsig.gdal.prov.ogr.OGRLibrary;
30
import org.gvsig.tools.library.AbstractLibrary;
31
import org.gvsig.tools.library.LibraryException;
32

  
33
/**
34
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
35
 *
36
 */
37
public class GMLLibrary extends AbstractLibrary {
38

  
39
    @Override
40
    public void doRegistration() {
41
        registerAsImplementationOf(GMLLibrary.class);
42
        require(OGRLibrary.class);
43
    }
44

  
45
    @Override
46
    protected void doInitialize() throws LibraryException {
47

  
48
    }
49

  
50
    @Override
51
    protected void doPostInitialize() throws LibraryException {
52

  
53
        // Register GML parameters definition
54
        FileHelper.registerParametersDefinition(GMLDataStoreParameters.PARAMETERS_DEFINITION_NAME,
55
            GMLDataStoreParameters.class, "GMLDataStoreParameters.xml");
56

  
57
        // Register data explorer parameters definition
58
        FileHelper.registerParametersDefinition(
59
            GMLDataExplorerParameters.PARAMETERS_DEFINITION_NAME, GMLDataExplorerParameters.class,
60
            "GMLExplorerParameters.xml");
61

  
62
        DataManagerProviderServices dataman =
63
            (DataManagerProviderServices) DALLocator.getDataManager();
64

  
65
        // Register GML provider factory
66
        if (!dataman.getStoreProviders().contains(GMLDataStoreProvider.NAME)) {
67
            dataman.registerStoreProviderFactory(new GMLDataStoreProviderFactory(
68
                GMLDataStoreProvider.NAME, GMLDataStoreProvider.DESCRIPTION));
69
        }
70

  
71
        // Register GML data explorer provider
72
        if (!dataman.getExplorerProviders().contains(GMLDataExplorer.NAME)) {
73
            dataman.registerExplorerProvider(GMLDataExplorer.NAME, GMLDataExplorer.class,
74
                GMLDataExplorerParameters.class);
75
        }
76

  
77
    }
78
}
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/java/org/gvsig/gdal/prov/gml/GMLDataStoreProviderFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.gml;
25

  
26
import java.io.File;
27

  
28
import org.gdal.ogr.DataSource;
29
import org.gdal.ogr.Driver;
30
import org.gdal.ogr.ogr;
31
import org.gvsig.fmap.dal.DataParameters;
32
import org.gvsig.fmap.dal.DataStoreParameters;
33
import org.gvsig.fmap.dal.DataStoreProvider;
34
import org.gvsig.fmap.dal.exception.InitializeException;
35
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
36
import org.gvsig.gdal.prov.ogr.BasicOGRDataStoreProviderFactory;
37
import org.gvsig.gdal.prov.ogr.OGRDataStoreProviderFactory;
38
import org.gvsig.tools.dynobject.DynObject;
39

  
40
/**
41
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
42
 *
43
 */
44
public class GMLDataStoreProviderFactory extends BasicOGRDataStoreProviderFactory implements
45
    OGRDataStoreProviderFactory {
46
    
47
    private static final String DRIVER_NAME = "GML";
48

  
49
    /**
50
     * @param name
51
     *            Name of factory
52
     * @param description
53
     *            Description of factory
54
     */
55
    public GMLDataStoreProviderFactory(String name, String description) {
56
        super(name, description);
57
    }
58

  
59
    @Override
60
    public DataStoreProvider createProvider(DataParameters dataParameters,
61
        DataStoreProviderServices providerServices) throws InitializeException {
62
        return new GMLDataStoreProvider((DataStoreParameters) dataParameters, providerServices);
63
    }
64

  
65
    @Override
66
    public DynObject createParameters() {
67
        return new GMLDataStoreParameters();
68
    }
69

  
70
    @Override
71
    public DynObject createParameters(Object connectionObject) {
72
        if (canOpen(connectionObject)) {
73
            return new GMLDataStoreParameters();
74
        }
75
        return null;
76
    }
77

  
78
    @Override
79
    public DynObject createDataExplorerParameters(Object connectionObject) {
80
        if (canOpen(connectionObject)) {
81
            return new GMLDataExplorerParameters();
82
        }
83
        return null;
84
    }
85

  
86
    private boolean canOpen(Object connectionObject) {
87
        
88
        if (connectionObject instanceof File) {
89
            File file = (File) connectionObject;
90
            if (file.isFile() && file.exists()) {
91
                Driver driver = ogr.GetDriverByName(getDriver());
92
                DataSource dataSource = driver.Open(file.getAbsolutePath());
93
                if (dataSource != null) {
94
                    dataSource.delete();
95
                    dataSource = null;
96
                    return true;
97
                }
98
            }
99
        } else if (connectionObject instanceof String) {
100
            String connectionString = (String) connectionObject;
101
            Driver driver = ogr.GetDriverByName(getDriver());
102
            DataSource dataSource = driver.Open(connectionString);
103
            if (dataSource != null) {
104
                dataSource.delete();
105
                dataSource = null;
106
                return true;
107
            }
108
        } else {
109
            throw new IllegalArgumentException(
110
                "Connection object must be a file or a connection string");
111
        }
112
        return false;
113
    }
114
    
115
    @Override
116
    public String getDriver() {
117
        return DRIVER_NAME;
118
    }
119
}
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/java/org/gvsig/gdal/prov/gml/GMLDataExplorer.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.gml;
25

  
26
import java.io.File;
27
import java.util.ArrayList;
28
import java.util.List;
29

  
30
import org.gdal.gdal.gdal;
31
import org.gdal.ogr.DataSource;
32
import org.gdal.ogr.Layer;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataManager;
35
import org.gvsig.fmap.dal.NewDataStoreParameters;
36
import org.gvsig.fmap.dal.exception.DataException;
37
import org.gvsig.fmap.dal.exception.InitializeException;
38
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
39
import org.gvsig.gdal.prov.ogr.OGRDataExplorer;
40
import org.gvsig.gdal.prov.ogr.OGRDataExplorerParameters;
41
import org.gvsig.gdal.prov.ogr.OGRDataStoreProvider;
42
import org.gvsig.gdal.prov.ogr.OGRNewDataStoreParameters;
43
import org.gvsig.gdal.prov.ogr.OGRUnsupportedFormatException;
44

  
45
/**
46
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
47
 *
48
 */
49
public class GMLDataExplorer extends OGRDataExplorer {
50

  
51
    /**
52
     * 
53
     */
54
    public static final String NAME = "GMLDataExplorer";
55

  
56
    /**
57
     * @param parameters
58
     *            Parameters to open data explorer.
59
     * @param providerServices
60
     *            Available services
61
     * @throws InitializeException
62
     *             If there is some error initializating this store provider
63
     */
64
    public GMLDataExplorer(OGRDataExplorerParameters parameters,
65
        DataServerExplorerProviderServices providerServices) throws InitializeException {
66
        super(parameters, providerServices);
67
    }
68

  
69
    @Override
70
    protected synchronized DataSource getDataSource() throws OGRUnsupportedFormatException {
71
        try {
72
            if (this.parameters instanceof GMLDataExplorerParameters) {
73
                GMLDataExplorerParameters gmlDataExplorerParameters =
74
                    (GMLDataExplorerParameters) this.parameters;
75

  
76
                if (gmlDataExplorerParameters.getGfsSchema() != null) {
77
                    gdal.SetConfigOption("GML_GFS_TEMPLATE", gmlDataExplorerParameters
78
                        .getGfsSchema().getAbsolutePath());
79
                }
80

  
81
                return super.getDataSource();
82
            } else {
83
                throw new IllegalStateException(
84
                    "Parameters of GML Data explorer is not instance of GMLDataExplorerParameters");
85
            }
86
        } finally {
87
            gdal.SetConfigOption("GML_GFS_TEMPLATE", null);
88
        }
89
    }
90

  
91
    @Override
92
    public NewDataStoreParameters getAddParameters(String storeName) throws DataException {
93
        if (getDataStoreProviderNames().contains(storeName)) {
94

  
95
            NewDataStoreParameters newDataStoreParameters = super.getAddParameters(storeName);
96
            if (OGRDataStoreProvider.NAME.equals(storeName)) {
97

  
98
                return newDataStoreParameters;
99
            } else if (GMLDataStoreProvider.NAME.equals(storeName)
100
                && newDataStoreParameters instanceof OGRNewDataStoreParameters) {
101

  
102
                ((OGRNewDataStoreParameters) newDataStoreParameters).setDriverName("GML");
103
                return newDataStoreParameters;
104
            }
105
        }
106

  
107
        return null;
108
    }
109

  
110
    @SuppressWarnings("unchecked")
111
    @Override
112
    public List getDataStoreProviderNames() {
113
        List<String> providerNames = super.getDataStoreProviderNames();
114
        providerNames.add(GMLDataStoreProvider.NAME);
115
        return providerNames;
116
    }
117

  
118
    @Override
119
    public List list(int mode) throws DataException {
120

  
121
        List<Layer> layerList = getLayerList();
122
        List<GMLDataStoreParameters> parametersList = new ArrayList<GMLDataStoreParameters>();
123
        DataManager dataManager = DALLocator.getDataManager();
124
        GMLDataExplorerParameters gmlDataExplorerParameters =
125
            (GMLDataExplorerParameters) this.parameters;
126

  
127
        for (Layer layer : layerList) {
128

  
129
            GMLDataStoreParameters storeParameters =
130
                (GMLDataStoreParameters) dataManager
131
                    .createStoreParameters(GMLDataStoreProvider.NAME);
132
            String connectionString = gmlDataExplorerParameters.getConnectionString();
133
            File file = gmlDataExplorerParameters.getFile();
134
            File gfsSchema = gmlDataExplorerParameters.getGfsSchema();
135
            storeParameters.setConnectionString(connectionString);
136
            storeParameters.setFile(file);
137
            storeParameters.setGfsSchema(gfsSchema);
138
            storeParameters.setLayerName(layer.GetName());
139
            parametersList.add(storeParameters);
140
        }
141

  
142
        return parametersList;
143
    }
144
}
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/java/org/gvsig/gdal/prov/gml/GMLDataStoreParameters.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.gml;
25

  
26
import java.io.File;
27

  
28
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
29
import org.gvsig.gdal.prov.ogr.OGRDataStoreParameters;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DelegatedDynObject;
32

  
33
/**
34
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
35
 *
36
 */
37
public class GMLDataStoreParameters extends OGRDataStoreParameters {
38
    
39
    /**
40
     * 
41
     */
42
    public static final String PARAMETERS_DEFINITION_NAME = "GMLDataStoreParameters";
43
    
44
    /**
45
     * 
46
     */
47
    public static final String XSD_SCHEMA_PARAMTER_NAME = "xsdSchema";
48

  
49
    /**
50
     * 
51
     */
52
    public static final String GFS_SCHEMA_PARAMTER_NAME = "gfsSchema";
53
    
54
    /**
55
     * 
56
     */
57
    public GMLDataStoreParameters() {
58
        this.parameters = (DelegatedDynObject) ToolsLocator.getDynObjectManager().createDynObject(
59
            ToolsLocator.getPersistenceManager().getDefinition(PARAMETERS_DEFINITION_NAME));
60
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME,
61
            GMLDataStoreProvider.NAME);
62
    }
63
    
64
    /**
65
     * @return Schema of this GML Store provider
66
     */
67
    public File getXsdSchema() {
68
        return (File) this.getDynValue(XSD_SCHEMA_PARAMTER_NAME);
69
    }
70

  
71
    /**
72
     * @param schema
73
     *            Schema of this GML Store provider
74
     */
75
    public void setXsdSchema(File schema) {
76
        this.setDynValue(XSD_SCHEMA_PARAMTER_NAME, schema);
77
    }
78

  
79
    /**
80
     * @return Schema of this GML Store provider
81
     */
82
    public File getGfsSchema() {
83
        return (File) this.getDynValue(GFS_SCHEMA_PARAMTER_NAME);
84
    }
85

  
86
    /**
87
     * @param schema
88
     *            Schema of this GML Store provider
89
     */
90
    public void setGfsSchema(File schema) {
91
        this.setDynValue(GFS_SCHEMA_PARAMTER_NAME, schema);
92
    }
93
    
94
    @Override
95
    public String getDataStoreName() {
96
        return GMLDataStoreProvider.NAME;
97
    }
98

  
99
    @Override
100
    public String getDescription() {
101
        return GMLDataStoreProvider.DESCRIPTION;
102
    }
103

  
104
}
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/java/org/gvsig/gdal/prov/gml/GMLDataExplorerParameters.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.gml;
25

  
26
import java.io.File;
27

  
28
import org.gvsig.fmap.dal.DataServerExplorerParameters;
29
import org.gvsig.gdal.prov.ogr.OGRDataExplorerParameters;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DelegatedDynObject;
32

  
33
/**
34
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
35
 *
36
 */
37
public class GMLDataExplorerParameters extends OGRDataExplorerParameters implements
38
    DataServerExplorerParameters {
39

  
40
    /**
41
     * 
42
     */
43
    public static final String PARAMETERS_DEFINITION_NAME = "GMLDataExplorerParameters";
44

  
45
    /**
46
     * 
47
     */
48
    public static final String GFS_SCHEMA_PARAMETER_NAME = "gfsSchema";
49

  
50
    private DelegatedDynObject parameters;
51

  
52
    /**
53
     * 
54
     */
55
    public GMLDataExplorerParameters() {
56
        this.parameters =
57
            (DelegatedDynObject) ToolsLocator.getDynObjectManager().createDynObject(
58
                ToolsLocator.getPersistenceManager().getDefinition(PARAMETERS_DEFINITION_NAME));
59
    }
60

  
61
    @Override
62
    public String getExplorerName() {
63
        return GMLDataExplorer.NAME;
64
    }
65

  
66
    @Override
67
    protected DelegatedDynObject getDelegatedDynObject() {
68
        return this.parameters;
69
    }
70

  
71
    /**
72
     * 
73
     * @return File
74
     */
75
    public File getGfsSchema() {
76
        return (File) this.getDynValue(GFS_SCHEMA_PARAMETER_NAME);
77
    }
78

  
79
    /**
80
     * @param schema
81
     */
82
    public void setGfsSchema(File schema) {
83
        this.setDynValue(GFS_SCHEMA_PARAMETER_NAME, schema);
84
    }
85
}
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/java/org/gvsig/gdal/prov/gml/GMLDataStoreProvider.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.gml;
25

  
26
import java.io.File;
27
import java.io.IOException;
28
import java.util.ArrayList;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Vector;
32

  
33
import org.apache.commons.io.FileUtils;
34
import org.apache.commons.io.FilenameUtils;
35
import org.apache.commons.lang3.StringUtils;
36
import org.cresques.cts.ICRSFactory;
37
import org.cresques.cts.IProjection;
38
import org.gdal.gdal.gdal;
39
import org.gdal.ogr.DataSource;
40
import org.gdal.ogr.Driver;
41
import org.gdal.ogr.FeatureDefn;
42
import org.gdal.ogr.FieldDefn;
43
import org.gdal.ogr.GeomFieldDefn;
44
import org.gdal.ogr.Layer;
45
import org.gdal.ogr.ogr;
46
import org.gdal.ogr.ogrConstants;
47
import org.gdal.osr.SpatialReference;
48
import org.gvsig.fmap.dal.DataStore;
49
import org.gvsig.fmap.dal.DataStoreParameters;
50
import org.gvsig.fmap.dal.exception.CopyException;
51
import org.gvsig.fmap.dal.exception.CreateException;
52
import org.gvsig.fmap.dal.exception.DataException;
53
import org.gvsig.fmap.dal.exception.InitializeException;
54
import org.gvsig.fmap.dal.exception.WriteException;
55
import org.gvsig.fmap.dal.feature.Feature;
56
import org.gvsig.fmap.dal.feature.FeatureSet;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.fmap.dal.feature.FeatureType;
59
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
60
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
61
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
62
import org.gvsig.fmap.geom.type.GeometryType;
63
import org.gvsig.gdal.prov.ogr.OGRConverter;
64
import org.gvsig.gdal.prov.ogr.OGRDataStoreProvider;
65
import org.gvsig.gdal.prov.ogr.OGRUnsupportedFormatException;
66
import org.gvsig.tools.dispose.DisposableIterator;
67
import org.gvsig.tools.dynobject.DynObject;
68
import org.gvsig.tools.exception.BaseException;
69
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71

  
72
/**
73
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
74
 *
75
 */
76
public class GMLDataStoreProvider extends OGRDataStoreProvider {
77

  
78
    private static final Logger LOG = LoggerFactory.getLogger(GMLDataStoreProvider.class);
79

  
80
    /**
81
     *
82
     */
83
    public static final String NAME = "GMLDataStoreProvider";
84

  
85
    /**
86
     *
87
     */
88
    public static final String DESCRIPTION = "GML provider to open vectorial resources";
89

  
90
    protected GMLDataStoreProvider(DataStoreParameters dataParameters,
91
        DataStoreProviderServices storeServices, DynObject metadata) throws InitializeException {
92
        super(dataParameters, storeServices, metadata);
93
    }
94

  
95
    protected GMLDataStoreProvider(DataStoreParameters dataParameters,
96
        DataStoreProviderServices storeServices) throws InitializeException {
97
        super(dataParameters, storeServices);
98
    }
99

  
100
    @Override
101
    protected synchronized DataSource getDataSource() throws OGRUnsupportedFormatException {
102

  
103
        try {
104

  
105
            if (this.dataSource == null) {
106

  
107
                if (getGMLParameters().getXsdSchema() != null
108
                    && StringUtils.isBlank(getGMLParameters().getConnectionString())) {
109
                    StringBuilder stb = new StringBuilder();
110
                    if (StringUtils.isBlank(getGMLParameters().getConnectionString())
111
                        && getGMLParameters().getFile() != null) {
112
                        stb.append(getGMLParameters().getFile().getAbsolutePath());
113

  
114
                    } else if (StringUtils.isNotBlank(getGMLParameters().getConnectionString())) {
115
                        stb.append(getGMLParameters().getConnectionString());
116
                    }
117

  
118
                    stb.append(",xsd=");
119
                    stb.append(getGMLParameters().getXsdSchema());
120

  
121
                    getGMLParameters().setConnectionString(stb.toString());
122
                }
123

  
124
                if (getGMLParameters().getGfsSchema() != null) {
125
                    gdal.SetConfigOption("GML_GFS_TEMPLATE", getGMLParameters().getGfsSchema()
126
                        .getAbsolutePath());
127
                }
128
            }
129

  
130
            return super.getDataSource();
131

  
132
        } finally {
133
            gdal.SetConfigOption("GML_GFS_TEMPLATE", null);
134
        }
135
    }
136

  
137
    private GMLDataStoreParameters getGMLParameters() {
138
        return (GMLDataStoreParameters) this.getParameters();
139
    }
140

  
141
    @Override
142
    public boolean allowWrite() {
143
        return true;
144
    }
145

  
146
    @SuppressWarnings("rawtypes")
147
    @Override
148
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds,
149
        Iterator featureTypesChanged) throws DataException {
150

  
151
        FeatureStore store = this.getStoreServices().getFeatureStore();
152
        FeatureType defaultFeatureType = store.getDefaultFeatureType();
153

  
154
        String baseName = FilenameUtils.getBaseName(getGMLParameters().getFile().getName());
155
        File newFile = null;
156
        File newSchemaFile = null;
157
        try {
158
            newFile = File.createTempFile(baseName, null);
159
            newSchemaFile = getSchemaFile(newFile);
160
        } catch (IOException e) {
161
            String msg = String.format("Temp %1s file", new Object[] { baseName });
162
            throw new CreateException(msg, e);
163
        }
164

  
165
        Driver driver = ogr.GetDriverByName("GML");
166
        Vector<String> options = new Vector<String>(1);
167
        options.add("FORMAT=GML3.2");
168

  
169
        DataSource newDataSource = driver.CreateDataSource(newFile.getAbsolutePath(), options);
170

  
171
        // Copy other layer to new data source
172
        OGRConverter converter = new OGRConverter();
173
        for (int i = 0; i < getDataSource().GetLayerCount(); i++) {
174
            Layer tmpLayer = getDataSource().GetLayerByIndex(i);
175

  
176
            if (tmpLayer.GetName().equals(getGMLParameters().getLayerName())) {
177
                continue;
178
            }
179

  
180
            SpatialReference srs = tmpLayer.GetSpatialRef();
181
            int ogrGeomType = tmpLayer.GetGeomType();
182

  
183
            List<FieldDefn> fields = new ArrayList<FieldDefn>();
184
            FeatureDefn layerDefn = tmpLayer.GetLayerDefn();
185
            for (int j = 0; j < layerDefn.GetFieldCount(); j++) {
186
                fields.add(layerDefn.GetFieldDefn(j));
187
            }
188

  
189
            List<GeomFieldDefn> geomFields = new ArrayList<GeomFieldDefn>();
190
            for (int j = 0; j < layerDefn.GetGeomFieldCount(); j++) {
191
                geomFields.add(layerDefn.GetGeomFieldDefn(j));
192
            }
193

  
194
            List<org.gdal.ogr.Feature> ogrFeatures = new ArrayList<org.gdal.ogr.Feature>();
195
            org.gdal.ogr.Feature feature = tmpLayer.GetNextFeature();
196
            while(feature!=null){
197
                ogrFeatures.add(feature);
198
            }
199
            createLayer(newDataSource, tmpLayer.GetName(), srs, ogrGeomType, fields, geomFields,
200
                ogrFeatures);
201
        }
202

  
203
        SpatialReference srs = getLayer().GetSpatialRef();
204
        if (srs == null) {
205
            IProjection projection = defaultFeatureType.getDefaultSRS();
206
            if (projection == null) {
207
                projection = (IProjection) this.getDynValue(DataStore.METADATA_CRS);
208
                if (projection != null) {
209
                    srs = new SpatialReference(projection.export(ICRSFactory.FORMAT_WKT));
210
                }
211
            }
212
        }
213

  
214
        int ogrGeomType = -1;
215
        GeometryType geomType = defaultFeatureType.getDefaultGeometryAttribute().getGeomType();
216
        if (geomType != null) {
217
            ogrGeomType = converter.convertToOGRGeomType(geomType.getType());
218
        }
219

  
220
        List<FieldDefn> fields = converter.convertFields(defaultFeatureType);
221
        List<GeomFieldDefn> geomFields = converter.convertGeometryFields(defaultFeatureType, true);
222
        FeatureSet featureSet = getFeatureStore().getFeatureSet();
223
        createLayer(newDataSource, getLayer().GetName(), srs, ogrGeomType, fields, geomFields,
224
            featureSet);
225

  
226
        // Release native resources to close file.
227
        newDataSource.delete();
228

  
229
        // Close current file
230
        getResource().removeConsumer(this);
231
        getResource().closeRequest();
232

  
233
        // Delete current file
234
        if (getGMLParameters().getFile().delete()) {
235

  
236
            try {
237
                FileUtils.copyFile(newFile, getGMLParameters().getFile());
238
                FileUtils.copyFile(newSchemaFile, getSchemaFile(getGMLParameters().getFile()));
239
            } catch (IOException e) {
240
                throw new CopyException(newFile.getName(), e);
241
            }
242
        }
243

  
244
        this.resourceProvider = null;
245
        getResource().addConsumer(this);
246

  
247
        getResource().notifyChanges();
248
    }
249

  
250
    private Layer createLayer(DataSource dataSource, String layerName, SpatialReference srs,
251
        int ogrGeomType, List<FieldDefn> fields, List<GeomFieldDefn> geomFields,
252
        FeatureSet featureSet) throws DataException {
253

  
254
        Layer newLayer = createLayer(dataSource, layerName, srs, ogrGeomType, fields, geomFields);
255

  
256
        OGRConverter converter = new OGRConverter();
257
        DisposableIterator iterator = featureSet.fastIterator();
258

  
259
        try {
260
            while (iterator.hasNext()) {
261
                Feature feature = (Feature) iterator.next();
262
                FeatureProvider featureProvider =
263
                    getStoreServices().getFeatureProviderFromFeature(feature);
264
                org.gdal.ogr.Feature orgFeature = converter.convert(featureProvider);
265
                newLayer.CreateFeature(orgFeature);
266
            }
267
        } catch (BaseException e) {
268
            LOG.error("Can not convert featureProvider to OGR Feature", e);
269
            throw new WriteException(newLayer.GetName(), e);
270
        } finally {
271
            iterator.dispose();
272
        }
273

  
274
        return newLayer;
275
    }
276

  
277
    private Layer createLayer(DataSource dataSource, String layerName, SpatialReference srs,
278
        int ogrGeomType, List<FieldDefn> fields, List<GeomFieldDefn> geomFields,
279
        List<org.gdal.ogr.Feature> ogrFeatures) throws DataException {
280

  
281
        Layer newLayer = createLayer(dataSource, layerName, srs, ogrGeomType, fields, geomFields);
282
        OGRConverter converter = new OGRConverter();
283
        // Save features to new layer
284
        for (org.gdal.ogr.Feature feature : ogrFeatures) {
285
            org.gdal.ogr.Feature fixedFeature = converter.convert(newLayer.GetLayerDefn(), feature);
286
            newLayer.CreateFeature(fixedFeature);
287
        }
288

  
289
        return newLayer;
290
    }
291

  
292
    private Layer createLayer(DataSource dataSource, String layerName, SpatialReference srs,
293
        int ogrGeomType, List<FieldDefn> fields, List<GeomFieldDefn> geomFields) {
294

  
295
        Layer newLayer = null;
296

  
297
        // Create new layer
298
        if (srs == null) {
299
            newLayer = dataSource.CreateLayer(layerName);
300
        } else if (ogrGeomType == -1) {
301
            newLayer = dataSource.CreateLayer(layerName, srs);
302
        } else {
303
            newLayer = dataSource.CreateLayer(layerName, srs, ogrGeomType);
304
        }
305

  
306
        // Creating fields
307
        if (newLayer.TestCapability(ogrConstants.OLCCreateField)) {
308
            for (FieldDefn fieldDefn : fields) {
309
                newLayer.CreateField(fieldDefn);
310
            }
311
        } else {
312
            LOG.warn("{} driver does not support creation of fields", dataSource.GetDriver()
313
                .getName());
314
        }
315

  
316
        // Creating geometry fields
317
        if (newLayer.TestCapability(ogrConstants.OLCCreateGeomField)) {
318
            for (GeomFieldDefn geomFieldDefn : geomFields) {
319
                newLayer.CreateGeomField(geomFieldDefn);
320
            }
321
        } else {
322
            LOG.warn("{} driver does not support creation of geometry fields, only default"
323
                + " OGR geometry field has been created", dataSource.GetDriver().getName());
324
        }
325

  
326
        return newLayer;
327
    }
328

  
329
    private File getSchemaFile(File newFile) {
330
        String baseName = FilenameUtils.getBaseName(newFile.getName());
331
        File xsdFiel = new File(newFile.getParentFile(), baseName + ".xsd");
332
        return xsdFiel;
333
    }
334

  
335
    public void resourceChanged(ResourceProvider resource) {
336

  
337
        if (getGMLParameters().getXsdSchema() == null) {
338
            File schemaFile = getSchemaFile(getGMLParameters().getFile());
339
            if(schemaFile.exists()){
340
                getGMLParameters().setXsdSchema(schemaFile);
341
            } else {
342
                getGMLParameters().setXsdSchema(null);
343
            }
344
        }
345

  
346
        FeatureStore store = this.getStoreServices().getFeatureStore();
347
        FeatureType defaultFeatureType = null;
348
        try {
349
            defaultFeatureType = store.getDefaultFeatureType();
350
            getGMLParameters().setDefaultGeometryField(
351
                defaultFeatureType.getDefaultGeometryAttributeName());
352
        } catch (DataException e) {
353
            LOG.error("Can not default featury type of {}", this.getName());
354
            return;
355
        }
356
        super.resourceChanged(resource);
357
    }
358
}
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.gdal.prov.gml.GMLLibrary
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/resources/org/gvsig/gdal/prov/gml/GMLExplorerParameters.xml
1
<?xml version="1.0"?>
2
<definitions>
3
	<version>1.0.0</version>
4
	<classes>
5
		<class name="GMLDataExplorerParameters">
6
			<description>Store the parameters needed to create GML data explorers
7
			</description>
8
			<extends>
9
				<class>OGRDataExplorerParameters</class>
10
			</extends>
11
			<fields>
12
				<field name="gfsSchema" type="file" mandatory="false" group="Basic">
13
					<description>GFS Schema definition to explorer GML</description>
14
				</field>
15
			</fields>
16
		</class>
17
	</classes>
18
</definitions>
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/src/main/resources/org/gvsig/gdal/prov/gml/GMLDataStoreParameters.xml
1
<?xml version="1.0"?>
2
<definitions>
3
	<version>1.0.0</version>
4
	<classes>
5
		<class name="GMLDataStoreParameters">
6
			<description>Store the parameters needed to open a GML store
7
			</description>
8
			<extends>
9
				<class>OGRDataStoreParameters</class>
10
			</extends>
11
			<fields>
12
				<field name="xsdSchema" type="file" mandatory="false" group="Basic">
13
					<description>XSD Schema of this GML store</description>
14
				</field>
15
				<field name="gfsSchema" type="file" mandatory="false" group="Basic">
16
					<description>GFS Schema of this GML store</description>
17
				</field>
18
			</fields>
19
		</class>
20
	</classes>
21
</definitions>  
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.gml/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
	<modelVersion>4.0.0</modelVersion>
3
	<parent>
4
		<groupId>org.gvsig</groupId>
5
		<artifactId>org.gvsig.gdal.prov</artifactId>
6
		<version>1.0.65</version>
7
	</parent>
8
	<artifactId>org.gvsig.gdal.prov.gml</artifactId>
9
	<name>org.gvsig.gdal.prov.gml</name>
10

  
11
	<dependencies>
12
		<dependency>
13
			<groupId>org.gvsig</groupId>
14
			<artifactId>org.gvsig.gdal.prov.ogr</artifactId>
15
		</dependency>
16
		<dependency>
17
			<groupId>org.gdal</groupId>
18
			<artifactId>gdal</artifactId>
19
		</dependency>
20
	</dependencies>
21

  
22
</project>
0 23

  
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.gdal.prov.ogr.OGRLibrary
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/resources/org/gvsig/gdal/prov/ogr/OGRDataStoreParameters.xml
1
<?xml version="1.0"?>
2
<definitions>
3
	<version>1.0.0</version>
4
	<classes>
5
		<class name="OGRDataStoreParameters">
6
			<extends>
7
				<class namespace="dal" name="ProviderParameters" />
8
			</extends>
9
			<description>Store the parameters needed to open OGR stores
10
			</description>
11
			<fields>
12
				<field name="CRS" type="crs" mandatory="false" group="Basic">
13
					<description>Coordinate reference system used</description>
14
				</field>
15
				<field name="file" type="file" mandatory="false" group="Basic">
16
					<description>Resource file</description>
17
				</field>
18
				<field name="connectionString" type="string" mandatory="false"
19
					group="Basic">
20
					<description>Connection string to open data source</description>
21
				</field>
22
				<field name="layerName" type="string" mandatory="false" group="Basic">
23
					<description>Layer name of data source</description>
24
				</field>
25
				<field name="defaultGeometryField" type="string" mandatory="false"
26
					group="Basic">
27
					<description>Name of default geometry field</description>
28
				</field>
29
        <field name="ignoreSpatialFilter" type="boolean" mandatory="false"
30
          group="Advanced" defaultValue="true">
31
          <description>Ignore spatial filter</description>
32
        </field>
33
			</fields>
34
		</class>
35
	</classes>
36
</definitions>
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/resources/org/gvsig/gdal/prov/ogr/OGRMetadata.xml
1
<?xml version="1.0"?>
2
<!--
3
Definitions of metadata fields of a shp file.  
4
 -->
5
<definitions>
6
  <version>1.0.0</version>
7
  <classes>
8
    
9
    <class name="OGRDataStoreProvider" namespace="Metadata">
10
      <extends>
11
      	<class namespace="Metadata" name="SpatialProvider"/>
12
      </extends>
13
      <description>Metadata of a OGR provider</description>
14
      <fields>
15
      </fields>
16
    </class>
17

  
18
  </classes>
19
</definitions>  
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/resources/org/gvsig/gdal/prov/ogr/OGRExplorerParameters.xml
1
<?xml version="1.0"?>
2
<definitions>
3
	<version>1.0.0</version>
4
	<classes>
5
		<class name="OGRDataExplorerParameters">
6
			<description>Store the parameters needed to create OGR data explorers
7
			</description>
8
			<fields>
9
				<field name="file" type="file" mandatory="false" group="Basic">
10
					<description>Resource file</description>
11
				</field>
12
				<field name="connectionString" type="string" mandatory="false"
13
					group="Basic">
14
					<description>Connection string to open data source</description>
15
				</field>
16
			</fields>
17
		</class>
18
	</classes>
19
</definitions>
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/resources/org/gvsig/gdal/prov/ogr/OGRNewDataStoreParameters.xml
1
<definitions>
2
	<version>1.0.0</version>
3
	<classes>
4
		<class name="OGRNewDataStoreParameters">
5
			<description>Store the parameters needed to create OGR data stores
6
			</description>
7
			<extends>
8
				<class namespace="dal" name="NewProviderParameters" />
9
				<class>OGRDataStoreParameters</class>
10
			</extends>
11
			<fields>
12
				<field name="driverName" type="string" mandatory="false" group="Basic">
13
					<description>Name of driver</description>
14
				</field>
15
				<field name="datasetCreationOptions" type="List" mandatory="false" group="Basic">
16
					<description>Name of driver</description>
17
				</field>
18
			</fields>
19
		</class>
20
	</classes>
21
</definitions>
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRDataStoreProviderFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.ogr;
25

  
26
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
27
import org.gvsig.tools.dynobject.DynObject;
28

  
29
/**
30
 * OGR Provider factory to create parameters to create specific server data
31
 * explorers and specific data store providers.
32
 * 
33
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
34
 *
35
 */
36
public interface OGRDataStoreProviderFactory extends FeatureStoreProviderFactory {
37

  
38
    /**
39
     * 
40
     * @return Name of driver used to open sources
41
     */
42
    public String getDriver();
43

  
44
    /**
45
     * Creates specific parameters if factory can open connection object. If it
46
     * can not open, it will be return null.
47
     * 
48
     * @param connectionObject
49
     *            File or connection string to be opened
50
     * @return Data explorer parameters If this factory can open connection
51
     *         object it will be return specific parameters, otherwise return
52
     *         null.
53
     */
54
    public DynObject createParameters(Object connectionObject);
55

  
56
    /**
57
     * Creates specific data explorer parameters if factory can open connection
58
     * object. If it
59
     * can not open, it will be return null.
60
     * 
61
     * @param connectionObject
62
     *            File or connection string to be opened
63
     * @return Data explorer parameters If this factory can open connection
64
     *         object it will be return specific data explrer parameters,
65
     *         otherwise return
66
     *         null.
67
     */
68
    public DynObject createDataExplorerParameters(Object connectionObject);
69

  
70
}
tags/org.gvsig.gdal-1.0.65/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRDataExplorer.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 gvSIG Association
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gdal.prov.ogr;
25

  
26
import java.io.File;
27
import java.util.ArrayList;
28
import java.util.List;
29

  
30
import org.apache.commons.lang3.StringUtils;
31
import org.cresques.cts.ICRSFactory;
32
import org.cresques.cts.IProjection;
33
import org.gdal.ogr.DataSource;
34
import org.gdal.ogr.FieldDefn;
35
import org.gdal.ogr.GeomFieldDefn;
36
import org.gdal.ogr.Layer;
37
import org.gdal.ogr.ogr;
38
import org.gdal.ogr.ogrConstants;
39
import org.gdal.osr.SpatialReference;
40
import org.gvsig.fmap.dal.DALLocator;
41
import org.gvsig.fmap.dal.DataManager;
42
import org.gvsig.fmap.dal.DataServerExplorerParameters;
43
import org.gvsig.fmap.dal.DataStoreParameters;
44
import org.gvsig.fmap.dal.NewDataStoreParameters;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.exception.InitializeException;
47
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
48
import org.gvsig.fmap.dal.feature.EditableFeatureType;
49
import org.gvsig.fmap.dal.resource.ResourceAction;
50
import org.gvsig.fmap.dal.resource.file.FileResource;
51
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
52
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
53
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
54
import org.gvsig.fmap.dal.spi.AbstractDataServerExplorer;
55
import org.gvsig.fmap.dal.spi.DataServerExplorerProvider;
56
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
57
import org.gvsig.tools.dynobject.DynField;
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

  
61
/**
62
 * 
63
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
64
 *
65
 */
66
public class OGRDataExplorer extends AbstractDataServerExplorer implements DataServerExplorerProvider, ResourceConsumer {
67

  
68
    private static final Logger LOG = LoggerFactory.getLogger(OGRDataExplorer.class);
69

  
70
    /**
71
     * 
72
     */
73
    public static final String NAME = "OGRDataExplorer";
74

  
75
    private DataServerExplorerProviderServices providerServives;
76
    protected OGRDataExplorerParameters parameters;
77

  
78
    private ResourceProvider resourceProvider;
79

  
80
    protected DataSource dataSource;
81

  
82
    protected Boolean updateSupport;
83

  
84
    private List<Layer> layerList;
85

  
86
    /**
87
     * @param parameters
88
     *            Parameters to open data explorer.
89
     * @param providerServices
90
     *            Available services
91
     * @throws InitializeException
92
     *             If there is some error initializing this store provider
93
     */
94
    public OGRDataExplorer(OGRDataExplorerParameters parameters,
95
        DataServerExplorerProviderServices providerServices) throws InitializeException {
96
        super(parameters, providerServices);
97
        this.providerServives = providerServices;
98
        this.parameters = parameters;
99

  
100
        getResourceProvider().addConsumer(this);
101
    }
102

  
103
    private ResourceProvider getResourceProvider() {
104
        if (this.resourceProvider == null) {
105
            ResourceManagerProviderServices manager =
106
                (ResourceManagerProviderServices) DALLocator.getResourceManager();
107

  
108
            if (StringUtils.isBlank(parameters.getConnectionString())) {
109
                try {
110
                    this.resourceProvider =
111
                        (FileResource) manager.createAddResource(FileResource.NAME,
112
                            new Object[] { this.parameters.getFile().getAbsolutePath() });
113
                } catch (InitializeException e) {
114
                    throw new ReadRuntimeException(getProviderName(), e);
115
                }
116
            } else {
117
                try {
118
                    this.resourceProvider =
119
                        (OGRResource) manager.createAddResource(OGRResource.NAME,
120
                            new Object[] { this.parameters.getConnectionString() });
121
                } catch (InitializeException e) {
122
                    throw new ReadRuntimeException(getProviderName(), e);
123
                }
124
            }
125
        }
126
        return this.resourceProvider;
127
    }
128

  
129
    protected synchronized DataSource getDataSource() throws OGRUnsupportedFormatException {
130
        if (this.dataSource == null) {
131
            // Prioritize connection string over file
132
            if (StringUtils.isNotBlank(this.parameters.getConnectionString())) {
133
                // Trying to open in update mode
134
                this.dataSource = ogr.Open(this.parameters.getConnectionString(), 1);
135

  
136
                if (this.dataSource == null) {
137
                    this.dataSource = ogr.Open(this.parameters.getConnectionString());
138
                    updateSupport = false;
139
                } else {
140
                    updateSupport = true;
141
                }
142

  
143
            } else if (this.parameters.getFile() != null && this.parameters.getFile().exists()) {
144

  
145
                // Trying to open in update mode
146
                this.dataSource = ogr.Open(this.parameters.getFile().getAbsolutePath(), 1);
147

  
148
                if (this.dataSource == null) {
149
                    this.dataSource = ogr.Open(this.parameters.getFile().getAbsolutePath());
150
                    updateSupport = false;
151
                } else {
152
                    updateSupport = true;
153
                }
154

  
155
            } else {
156
                throw new IllegalStateException(
157
                    "Invalid parameters. Connection string must not be blank or file must exists");
158
            }
159
        }
160

  
161
        if (this.dataSource == null) {
162

  
163
            if (StringUtils.isNotBlank(this.parameters.getConnectionString())) {
164
                throw new OGRUnsupportedFormatException(this.parameters.getConnectionString());
165
            }
166
        }
167

  
168
        return this.dataSource;
169
    }
170

  
171
    @SuppressWarnings("unchecked")
172
    protected List<Layer> getLayerList() {
173
        if (this.layerList == null) {
174
            this.layerList = (List<Layer>) getResourceProvider().execute(new ResourceAction() {
175

  
176
                @Override
177
                public Object run() throws Exception {
178
                    List<Layer> layers = new ArrayList<Layer>();
179

  
180
                    for (int i = 0; i < getDataSource().GetLayerCount(); i++) {
181
                        layers.add(getDataSource().GetLayer(i));
182
                    }
183

  
184
                    return layers;
185
                }
186
            });
187
        }
188
        return this.layerList;
189
    }
190

  
191
    private Boolean hasUpdateSupport() throws OGRUnsupportedFormatException {
192
        if (this.updateSupport == null) {
193
            // Open data source to check update support of driver
194
            getDataSource();
195
        }
196
        return this.updateSupport;
197
    }
198

  
199
    @Override
200
    public String getProviderName() {
201
        return NAME;
202
    }
203

  
204
    @Override
205
    public boolean canAdd() {
206

  
207
        return (boolean) getResourceProvider().execute(new ResourceAction() {
208

  
209
            @Override
210
            public Object run() throws Exception {
211
                return getDataSource().TestCapability(ogrConstants.ODsCCreateLayer)
212
                    && getDataSource().TestCapability(ogrConstants.ODsCDeleteLayer)
213
                    && hasUpdateSupport();
214
            }
215
        });
216
    }
217

  
218
    @Override
219
    public boolean canAdd(String storeName) throws DataException {
220
        return getDataStoreProviderNames().contains(storeName) && canAdd();
221
    }
222

  
223
    @SuppressWarnings("rawtypes")
224
    @Override
225
    public List list() throws DataException {
226
        return list(MODE_ALL);
227
    }
228

  
229
    @SuppressWarnings("rawtypes")
230
    @Override
231
    public List list(int mode) throws DataException {
232
        List<Layer> layerList = getLayerList();
233
        DataManager dataManager = DALLocator.getDataManager();
234
        
235
        List<OGRDataStoreParameters> parametersList = new ArrayList<OGRDataStoreParameters>();
236
        for (Layer layer : layerList) {
237
            
238
            OGRDataStoreParameters storeParameters =
239
                (OGRDataStoreParameters) dataManager.createStoreParameters(OGRDataStoreProvider.NAME);
240
            String connectionString = this.parameters.getConnectionString();
241
            File file = this.parameters.getFile();
242
            storeParameters.setConnectionString(connectionString);
243
            storeParameters.setFile(file);
244
            storeParameters.setLayerName(layer.GetName());
245
            parametersList.add(storeParameters);
246
        }
247
        return parametersList;
248
    }
249

  
250
    @Override
251
    public boolean add(String provider, final NewDataStoreParameters parameters,
252
        final boolean overwrite) throws DataException {
253

  
254
        if (canAdd()) {
255

  
256
            return (boolean) getResourceProvider().execute(new ResourceAction() {
257

  
258
                @Override
259
                public Object run() throws Exception {
260

  
261
                    if (parameters instanceof OGRNewDataStoreParameters) {
262
                        OGRNewDataStoreParameters ogrParameters =
263
                            (OGRNewDataStoreParameters) parameters;
264

  
265
                        String layerName = ogrParameters.getLayerName();
266
                        if (overwrite) {
267
                            for (int i = 0; i < getLayerList().size(); i++) {
268
                                if (getLayerList().get(i).GetName().equals(layerName)) {
269
                                    getDataSource().DeleteLayer(i);
270
                                }
271
                            }
272
                        } else {
273
                            for (Layer layer : getLayerList()) {
274
                                if (layer.GetName().equals(layerName)) {
275
                                    return false;
276
                                }
277
                            }
278
                        }
279

  
280
                        EditableFeatureType editableFeatureType =
281
                            ogrParameters.getDefaultFeatureType();
282

  
283
                        OGRConverter converter = new OGRConverter();
284

  
285
                        // If SRS is defined convert it to spatial reference
286
                        IProjection projection = editableFeatureType.getDefaultSRS();
287
                        SpatialReference srs = null;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff