Revision 393

View differences:

tags/org.gvsig.gdal-1.0.60/org.gvsig.gdal.prov/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</artifactId>
6
		<version>1.0.60</version>
7
	</parent>
8
	<artifactId>org.gvsig.gdal.prov</artifactId>
9
	<packaging>pom</packaging>
10
	<name>org.gvsig.gdal.prov</name>
11
	<modules>
12
		<module>org.gvsig.gdal.prov.ogr</module>
13
		<module>org.gvsig.gdal.prov.gml</module>
14
	</modules>
15
</project>
tags/org.gvsig.gdal-1.0.60/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.60/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.60/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.60/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.60/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.60/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.60/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.60/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.60/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.60/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.60</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.60/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRDataExplorerParameters.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

  
28
import org.gvsig.fmap.dal.DataServerExplorerParameters;
29
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DelegatedDynObject;
32

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

  
41
    /**
42
     * 
43
     */
44
    public static final String PARAMETERS_DEFINITION_NAME = "OGRDataExplorerParameters";
45

  
46
    /**
47
     * 
48
     */
49
    public static final String FILE_PARAMETER_NAME = "file";
50

  
51
    /**
52
     * 
53
     */
54
    public static final String CONNECTION_STRING_PARAMETER_NAME = "connectionString";
55

  
56
    private DelegatedDynObject parameters;
57

  
58
    /**
59
     * 
60
     */
61
    public OGRDataExplorerParameters() {
62
        this.parameters = (DelegatedDynObject) ToolsLocator.getDynObjectManager().createDynObject(
63
            ToolsLocator.getPersistenceManager().getDefinition(PARAMETERS_DEFINITION_NAME));
64
    }
65

  
66
    @Override
67
    public String getExplorerName() {
68
        return OGRDataExplorer.NAME;
69
    }
70

  
71
    @Override
72
    protected DelegatedDynObject getDelegatedDynObject() {
73
        return this.parameters;
74
    }
75

  
76
    /**
77
     * 
78
     * @return File
79
     */
80
    public File getFile() {
81
        return (File) this.getDynValue(FILE_PARAMETER_NAME);
82
    }
83

  
84
    /**
85
     * 
86
     * @return String
87
     */
88
    public String getConnectionString() {
89
        return (String) this.getDynValue(CONNECTION_STRING_PARAMETER_NAME);
90
    }
91

  
92
    /**
93
     * 
94
     * @param file
95
     *            File
96
     */
97
    public void setFile(File file) {
98
        this.setDynValue(FILE_PARAMETER_NAME, file);
99
    }
100

  
101
    /**
102
     * 
103
     * @param connection
104
     *            Connection string
105
     */
106
    public void setConnectionString(String connection) {
107
        this.setDynValue(CONNECTION_STRING_PARAMETER_NAME, connection);
108
    }
109
}
tags/org.gvsig.gdal-1.0.60/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRUnsupportedFormatException.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.util.Map;
27

  
28
import org.gvsig.fmap.dal.exception.DataException;
29

  
30
/**
31
 * 
32
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
33
 *
34
 */
35
@SuppressWarnings("unchecked")
36
public class OGRUnsupportedFormatException extends DataException {
37

  
38
    private static final long serialVersionUID = -2670314598211236837L;
39

  
40
    private final static String MESSAGE =
41
        "Unsupported vectorial format. There are not any OGR driver registered to open %(connectionString)";
42

  
43
    private final static String KEY = "_UnsupportedVectorialFormatException";
44

  
45
    /**
46
     * @param messageFormat
47
     *            Message format
48
     * @param cause
49
     *            Cause
50
     * @param messageKey
51
     *            Message key
52
     * @param code
53
     *            code
54
     * @param values
55
     *            Values
56
     */
57
    public OGRUnsupportedFormatException(String messageFormat, Throwable cause, String messageKey,
58
        long code, Map<Object, Object> values) {
59
        super(messageFormat, cause, messageKey, code);
60
        this.values.putAll(values);
61
    }
62

  
63
    /**
64
     * @param messageFormat
65
     *            Message format
66
     * @param cause
67
     *            Cause
68
     * @param values
69
     *            Values
70
     */
71
    public OGRUnsupportedFormatException(String messageFormat, Throwable cause,
72
        Map<Object, Object> values) {
73
        super(messageFormat, cause, KEY, serialVersionUID);
74
        this.values.putAll(values);
75
    }
76

  
77
    /**
78
     * @param connectionString
79
     *            Conenction string
80
     */
81
    public OGRUnsupportedFormatException(String connectionString) {
82
        super(MESSAGE, KEY, serialVersionUID);
83
        this.values.put("connectionString", connectionString);
84
    }
85

  
86
}
tags/org.gvsig.gdal-1.0.60/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRDataStoreProvider.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.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29

  
30
import org.apache.commons.lang3.StringUtils;
31
import org.gdal.ogr.DataSource;
32
import org.gdal.ogr.Feature;
33
import org.gdal.ogr.FeatureDefn;
34
import org.gdal.ogr.FieldDefn;
35
import org.gdal.ogr.GeomFieldDefn;
36
import org.gdal.ogr.Geometry;
37
import org.gdal.ogr.Layer;
38
import org.gdal.ogr.ogr;
39
import org.gdal.ogr.ogrConstants;
40

  
41
import org.gvsig.fmap.dal.DataStore;
42
import org.gvsig.fmap.dal.DataStoreParameters;
43
import org.gvsig.fmap.dal.DataTypes;
44
import org.gvsig.fmap.dal.FileHelper;
45
import org.gvsig.fmap.dal.exception.DataException;
46
import org.gvsig.fmap.dal.exception.InitializeException;
47
import org.gvsig.fmap.dal.exception.OpenException;
48
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
49
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
50
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
51
import org.gvsig.fmap.dal.feature.FeatureQuery;
52
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
53
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
54
import org.gvsig.fmap.dal.feature.FeatureType;
55
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
56
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
57
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
58
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
59
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
60
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
61
import org.gvsig.fmap.dal.resource.ResourceAction;
62
import org.gvsig.fmap.dal.resource.file.FileResource;
63
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
64
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
65
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
66
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
67
import org.gvsig.fmap.geom.GeometryLocator;
68
import org.gvsig.fmap.geom.primitive.Envelope;
69
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
70
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
71
import org.gvsig.tools.dynobject.DynObject;
72
import org.gvsig.tools.evaluator.Evaluator;
73
import org.gvsig.tools.exception.BaseException;
74

  
75
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
77

  
78
/**
79
 *
80
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
81
 *
82
 */
83
public class OGRDataStoreProvider extends AbstractFeatureStoreProvider implements
84
    FeatureStoreProvider, ResourceConsumer {
85

  
86
    private static final Logger LOG = LoggerFactory.getLogger(OGRDataStoreProvider.class);
87

  
88
    /**
89
     *
90
     */
91
    public static final String METADATA_DEFINITION_NAME = "OGRDataStoreProvider";
92

  
93
    /**
94
     *
95
     */
96
    public static final String NAME = "OGRDataStoreProvider";
97

  
98
    /**
99
     *
100
     */
101
    public static final String DESCRIPTION = "OGR provider to open vectorial resources";
102

  
103
    protected DataSource dataSource;
104

  
105
    private Envelope envelope;
106

  
107
    private Layer newLayer;
108

  
109
    protected ResourceProvider resourceProvider;
110

  
111
    private Boolean updateSupport;
112

  
113
    private boolean opened = false;
114

  
115
    protected OGRDataStoreProvider(DataStoreParameters dataParameters,
116
        DataStoreProviderServices storeServices, DynObject metadata) throws InitializeException {
117
        super(dataParameters, storeServices, metadata);
118

  
119
        // Set CRS parameter to metadata
120
        this.setDynValue(DataStore.METADATA_CRS, dataParameters.getDynValue(DataStore.METADATA_CRS));
121

  
122
        getResource().addConsumer(this);
123

  
124
        try {
125
            this.open();
126
        } catch (OpenException e) {
127
            throw new InitializeException(NAME, e);
128
        }
129
    }
130

  
131
    protected OGRDataStoreProvider(DataStoreParameters dataParameters,
132
        DataStoreProviderServices storeServices) throws InitializeException {
133
        this(dataParameters, storeServices, FileHelper
134
            .newMetadataContainer(METADATA_DEFINITION_NAME));
135
    }
136

  
137
    /*
138
     * Lazy initialization of data source
139
     */
140
    protected synchronized DataSource getDataSource() throws OGRUnsupportedFormatException {
141
        if (this.dataSource == null) {
142

  
143
            // Prioritize connection string over file
144
            if (StringUtils.isNotBlank(getOGRParameters().getConnectionString())) {
145

  
146
                // Trying to open in update mode
147
                this.dataSource = ogr.Open(getOGRParameters().getConnectionString(), 1);
148

  
149
                if (this.dataSource == null) {
150
                    this.dataSource = ogr.Open(getOGRParameters().getConnectionString());
151
                    updateSupport = false;
152
                } else {
153
                    updateSupport = true;
154
                }
155

  
156
            } else if (getOGRParameters().getFile() != null
157
                && getOGRParameters().getFile().exists()) {
158

  
159
                // Trying to open in update mode
160
                this.dataSource = ogr.Open(getOGRParameters().getFile().getAbsolutePath(), 1);
161

  
162
                if (this.dataSource == null) {
163
                    this.dataSource = ogr.Open(getOGRParameters().getFile().getAbsolutePath());
164
                    updateSupport = false;
165
                } else {
166
                    updateSupport = true;
167
                }
168

  
169
            } else {
170
                throw new IllegalStateException(
171
                    "Invalid parameters. Connection string must not be blank or file must exists");
172
            }
173
        }
174

  
175
        if (this.dataSource == null) {
176

  
177
            if (StringUtils.isNotBlank(getOGRParameters().getConnectionString())) {
178
                throw new OGRUnsupportedFormatException(getOGRParameters().getConnectionString());
179
            }
180
        }
181

  
182
        return this.dataSource;
183
    }
184

  
185
    /*
186
     * Lazy initialization of update support flag
187
     */
188
    private Boolean hasUpdateSupport() throws OGRUnsupportedFormatException {
189
        if (this.updateSupport == null) {
190
            getDataSource();
191
        }
192
        return this.updateSupport;
193
    }
194

  
195
    /*
196
     * Lazy initialization of layer
197
     */
198
    protected Layer getLayer() throws OGRUnsupportedFormatException {
199
        if (this.newLayer == null) {
200
            this.newLayer = getDataSource().GetLayer(getOGRParameters().getLayerName());
201
            // this.layer = getDataSource().GetLayer(0);
202
            if (this.newLayer == null) {
203
                LOG.warn("Can not get layer with {} name. Get first layer of data source",
204
                    getOGRParameters().getLayerName());
205
                this.newLayer = getDataSource().GetLayer(0);
206
                getOGRParameters().setLayerName(this.newLayer.GetName());
207
            }
208
        }
209
        return this.newLayer;
210
    }
211

  
212
    /*
213
     * Lazy envelope initialization
214
     */
215
    @Override
216
    public Envelope getEnvelope() throws DataException {
217
        open();
218
        if (this.envelope == null) {
219
            this.envelope = (Envelope) getResource().execute(new ResourceAction() {
220

  
221
                @Override
222
                public Object run() throws Exception {
223
                    Layer layer = getLayer();
224
                    double[] extent = layer.GetExtent(true);
225
                    if (extent != null) {
226
                        return GeometryLocator.getGeometryManager().createEnvelope(extent[0],
227
                            extent[2], extent[1], extent[3], SUBTYPES.GEOM2D);
228
                    } else {
229
                        Envelope tmpEnvelope =
230
                            GeometryLocator.getGeometryManager().createEnvelope(SUBTYPES.GEOM2D);
231
                        FeatureType featureType = getStoreServices().getDefaultFeatureType();
232
                        layer.ResetReading();
233
                        Feature feature = layer.GetNextFeature();
234
                        while (feature!=null) {
235
                            double[] envelope = new double[4];
236
                            int geomFieldIndex =
237
                                layer.GetLayerDefn().GetGeomFieldIndex(
238
                                    featureType.getDefaultGeometryAttributeName());
239
                            Geometry ogrGeometry = feature.GetGeomFieldRef(geomFieldIndex);
240
                            ogrGeometry.GetEnvelope(envelope);
241
                            tmpEnvelope.add(GeometryLocator.getGeometryManager()
242
                                .createEnvelope(envelope[0], envelope[2], envelope[1], envelope[3],
243
                                    SUBTYPES.GEOM2D));
244
                            feature = layer.GetNextFeature();
245
                        }
246

  
247
                        return tmpEnvelope;
248
                    }
249
                }
250
            });
251
        }
252
        return this.envelope;
253
    }
254

  
255
    @Override
256
    public String getFullName() {
257

  
258
        StringBuilder stb = new StringBuilder();
259
        stb.append(NAME);
260
        stb.append(":");
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff