Revision 165

View differences:

tags/org.gvsig.gdal-1.0.20/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.20/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.20/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
			</fields>
30
		</class>
31
	</classes>
32
</definitions>  
tags/org.gvsig.gdal-1.0.20/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.20/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.20/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.20/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.20/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
import org.gvsig.fmap.dal.DataStore;
41
import org.gvsig.fmap.dal.DataStoreParameters;
42
import org.gvsig.fmap.dal.DataTypes;
43
import org.gvsig.fmap.dal.FileHelper;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.exception.InitializeException;
46
import org.gvsig.fmap.dal.exception.OpenException;
47
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
48
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
49
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
50
import org.gvsig.fmap.dal.feature.FeatureQuery;
51
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
52
import org.gvsig.fmap.dal.feature.FeatureQueryOrder.FeatureQueryOrderMember;
53
import org.gvsig.fmap.dal.feature.FeatureType;
54
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
55
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
56
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
57
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
58
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
59
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
60
import org.gvsig.fmap.dal.resource.ResourceAction;
61
import org.gvsig.fmap.dal.resource.file.FileResource;
62
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
63
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
64
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
65
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
66
import org.gvsig.fmap.geom.GeometryLocator;
67
import org.gvsig.fmap.geom.primitive.Envelope;
68
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
69
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
70
import org.gvsig.tools.dynobject.DynObject;
71
import org.gvsig.tools.evaluator.Evaluator;
72
import org.gvsig.tools.exception.BaseException;
73
import org.slf4j.Logger;
74
import org.slf4j.LoggerFactory;
75

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

  
84
    private static final Logger LOG = LoggerFactory.getLogger(OGRDataStoreProvider.class);
85

  
86
    /**
87
     * 
88
     */
89
    public static final String METADATA_DEFINITION_NAME = "OGRDataStoreProvider";
90

  
91
    /**
92
     * 
93
     */
94
    public static final String NAME = "OGRDataStoreProvider";
95

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

  
101
    protected DataSource dataSource;
102

  
103
    private Envelope envelope;
104

  
105
    private Layer newLayer;
106

  
107
    protected ResourceProvider resourceProvider;
108

  
109
    private Boolean updateSupport;
110

  
111
    private boolean opened = false;
112

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

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

  
120
        getResource().addConsumer(this);
121

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

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

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

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

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

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

  
154
            } else if (getOGRParameters().getFile() != null
155
                && getOGRParameters().getFile().exists()) {
156

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

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

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

  
173
        if (this.dataSource == null) {
174

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

  
180
        return this.dataSource;
181
    }
182

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

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

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

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

  
244
                        return tmpEnvelope;
245
                    }
246
                }
247
            });
248
        }
249
        return this.envelope;
250
    }
251

  
252
    @Override
253
    public String getFullName() {
254

  
255
        StringBuilder stb = new StringBuilder();
256
        stb.append(NAME);
257
        stb.append(":");
258
        if (StringUtils.isBlank(getOGRParameters().getConnectionString())) {
259
            stb.append(getOGRParameters().getFile().getAbsolutePath());
260
            stb.append(":");
261
            stb.append(getOGRParameters().getLayerName());
262
        } else {
263
            stb.append(getOGRParameters().getConnectionString());
264
        }
265
        return stb.toString();
266
    }
267

  
268
    @Override
269
    public String getName() {
270
        return getOGRParameters().getLayerName();
271
    }
272

  
273
    @Override
274
    public String getProviderName() {
275
        return NAME;
276
    }
277

  
278
    @Override
279
    public boolean allowWrite() {
280
        try {
281
            return getLayer().TestCapability(ogrConstants.OLCAlterFieldDefn)
282
                && getLayer().TestCapability(ogrConstants.OLCCreateField)
283
                && getLayer().TestCapability(ogrConstants.OLCDeleteField)
284
                && getLayer().TestCapability(ogrConstants.OLCDeleteFeature) && hasUpdateSupport();
285
        } catch (OGRUnsupportedFormatException e) {
286
            LOG.error("Can not determinate if data source allows write", e);
287
            return false;
288
        }
289
    }
290

  
291
    @Override
292
    public ResourceProvider getResource() {
293

  
294
        if (this.resourceProvider == null) {
295
            if (StringUtils.isBlank(getOGRParameters().getConnectionString())) {
296
                try {
297
                    this.resourceProvider =
298
                        this.createResource(FileResource.NAME, new Object[] { getOGRParameters()
299
                            .getFile().getAbsolutePath() });
300
                } catch (InitializeException e) {
301
                    throw new ReadRuntimeException(String.format(
302
                        "Can not create file resource with %1s path", getOGRParameters().getFile()
303
                            .getAbsolutePath()), e);
304
                }
305
            } else {
306
                try {
307
                    this.resourceProvider =
308
                        this.createResource(OGRResource.NAME, new Object[] { getOGRParameters()
309
                            .getConnectionString() });
310
                } catch (InitializeException e) {
311
                    throw new ReadRuntimeException(String.format(
312
                        "Can not create OGR resource with %1s", getOGRParameters()
313
                            .getConnectionString()), e);
314
                }
315
            }
316
        }
317

  
318
        return resourceProvider;
319
    }
320

  
321
    @Override
322
    public Object getSourceId() {
323
        return this.getOGRParameters().getFile();
324
    }
325

  
326
    @Override
327
    public void open() throws OpenException {
328

  
329
        if (opened == false) {
330
            try {
331
                this.opened = loadFeatureType();
332
            } catch (BaseException e) {
333
                LOG.error("Can not load feature type", e);
334
                throw new OpenException(getFullName(), e);
335
            }
336
        }
337
    }
338

  
339
    protected boolean loadFeatureType() throws OGRUnsupportedFormatException,
340
        GeometryTypeNotSupportedException, GeometryTypeNotValidException {
341

  
342
        return (boolean) getResource().execute(new ResourceAction() {
343

  
344
            @Override
345
            public Object run() throws Exception {
346
                FeatureDefn featureDefn = getLayer().GetLayerDefn();
347
                OGRConverter converter = new OGRConverter();
348
                String defaultGeometryField = getOGRParameters().getDefaultGeometryField();
349
                FeatureType featureType = converter.convert(featureDefn, defaultGeometryField);
350

  
351
                if (featureType.getDefaultSRS() != null) {
352
                    setDynValue(DataStore.METADATA_CRS, featureType.getDefaultSRS());
353
                }
354

  
355
                List<FeatureType> featureTypes = new ArrayList<FeatureType>();
356
                featureTypes.add(featureType);
357

  
358
                getStoreServices().setFeatureTypes(featureTypes, featureType);
359
                return true;
360
            }
361
        });
362
    }
363

  
364
    @SuppressWarnings("rawtypes")
365
    @Override
366
    public void performChanges(final Iterator deleteds, final Iterator inserteds,
367
        final Iterator updateds, final Iterator featureTypesChanged) throws DataException {
368

  
369
        getResource().execute(new ResourceAction() {
370

  
371
            @Override
372
            public Object run() throws Exception {
373
                OGRConverter converter = new OGRConverter();
374

  
375
                if (getLayer().TestCapability(ogrConstants.OLCTransactions)) {
376
                    getLayer().StartTransaction();
377
                }
378

  
379
                while (featureTypesChanged.hasNext()) {
380
                    FeatureTypeChanged featureTypeChange =
381
                        (FeatureTypeChanged) featureTypesChanged.next();
382
                    FeatureType source = featureTypeChange.getSource();
383
                    FeatureType target = featureTypeChange.getTarget();
384

  
385
                    for (int i = 0; i < source.getAttributeDescriptors().length; i++) {
386
                        EditableFeatureAttributeDescriptor eAttDescriptor =
387
                            source.getEditable().getEditableAttributeDescriptor(i);
388

  
389
                        if (eAttDescriptor.getOriginalName() != null) {
390
                            int index =
391
                                getLayer().GetLayerDefn().GetFieldIndex(
392
                                    eAttDescriptor.getOriginalName());
393

  
394
                            FieldDefn field = converter.convertField(eAttDescriptor);
395
                            getLayer().AlterFieldDefn(index, field, ogrConstants.ALTER_ALL_FLAG);
396
                        } else if (target.getAttributeDescriptor(eAttDescriptor.getName()) == null) {
397
                            int index = getLayer().FindFieldIndex(eAttDescriptor.getName(), 1);
398
                            getLayer().DeleteField(index);
399
                        }
400
                    }
401

  
402
                    List<FieldDefn> fields = converter.convertFields(target);
403
                    for (FieldDefn fieldDefn : fields) {
404
                        int index = getLayer().GetLayerDefn().GetFieldIndex(fieldDefn.GetName());
405
                        if (index == -1) {
406
                            getLayer().CreateField(fieldDefn);
407
                        } else {
408
                            getLayer()
409
                                .AlterFieldDefn(index, fieldDefn, ogrConstants.ALTER_ALL_FLAG);
410
                        }
411
                    }
412

  
413
                    if (getLayer().TestCapability(ogrConstants.OLCCreateGeomField)) {
414
                        List<GeomFieldDefn> geometryFields =
415
                            converter.convertGeometryFields(target, true);
416
                        for (GeomFieldDefn geomFieldDefn : geometryFields) {
417
                            int index =
418
                                getLayer().GetLayerDefn()
419
                                    .GetGeomFieldIndex(geomFieldDefn.GetName());
420
                            if (index == -1) {
421
                                getLayer().CreateGeomField(geomFieldDefn);
422
                            }
423
                        }
424
                    } else {
425
                        StringBuilder stb = new StringBuilder();
426
                        stb.append("Driver '");
427
                        stb.append(getDataSource().GetDriver().GetName());
428
                        stb.append("' does not support create geometry fields");
429
                        LOG.warn(stb.toString());
430
                    }
431
                }
432

  
433
                while (deleteds.hasNext()) {
434
                    FeatureReferenceProviderServices reference =
435
                        (FeatureReferenceProviderServices) deleteds.next();
436
                    getLayer().DeleteFeature((int) reference.getOID());
437
                }
438

  
439
                while (inserteds.hasNext()) {
440
                    FeatureProvider featureProvider = (FeatureProvider) inserteds.next();
441
                    getLayer().CreateFeature(converter.convert(featureProvider));
442
                }
443

  
444
                while (updateds.hasNext()) {
445
                    FeatureProvider featureProvider = (FeatureProvider) updateds.next();
446
                    Feature ogrFeature = converter.convert(featureProvider);
447
                    getLayer().SetFeature(ogrFeature);
448
                }
449

  
450
                if (getLayer().TestCapability(ogrConstants.OLCTransactions)) {
451
                    getLayer().CommitTransaction();
452
                }
453
                getDataSource().SyncToDisk();
454
                repack();
455
                getResource().notifyChanges();
456

  
457
                return null;
458
            }
459
        });
460
    }
461

  
462
    protected void repack() throws OGRUnsupportedFormatException {
463
        LOG.debug("Running SQL: REPACK ".concat(getLayer().GetName()));
464
        getDataSource().ExecuteSQL("REPACK ".concat(getLayer().GetName()));
465
    }
466

  
467
    @Override
468
    public Object createNewOID() {
469
        try {
470
            return getFeatureCount() + 1;
471
        } catch (DataException e) {
472
            LOG.error("Can't get feature count", e);
473
            throw new ReadRuntimeException(getFullName(), e);
474
        }
475
    }
476

  
477
    @Override
478
    public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType)
479
        throws DataException {
480
        open();
481
        return new OGRFetureSetProvider(this, query, featureType);
482
    }
483

  
484
    @Override
485
    public long getFeatureCount() throws DataException {
486
        open();
487
        return ((Number) getResource().execute(new ResourceAction() {
488

  
489
            @Override
490
            public Object run() throws Exception {
491

  
492
                return getLayer().GetFeatureCount();
493
            }
494
        })).longValue();
495
    }
496

  
497
    @Override
498
    public int getOIDType() {
499
        return DataTypes.LONG;
500
    }
501

  
502
    @Override
503
    protected FeatureProvider internalGetFeatureProviderByReference(
504
        FeatureReferenceProviderServices providerServices, FeatureType featureType)
505
        throws DataException {
506

  
507
        Feature ogrFeature = getLayer().GetFeature((int) providerServices.getOID());
508
        FeatureProvider featureProvider =
509
            new DefaultFeatureProvider(featureType, ogrFeature.GetFID());
510
        OGRConverter converter = new OGRConverter();
511
        featureProvider = converter.convert(featureProvider, featureType, ogrFeature);
512
        return featureProvider;
513
    }
514

  
515
    private OGRDataStoreParameters getOGRParameters() {
516
        return (OGRDataStoreParameters) this.getParameters();
517
    }
518

  
519
    @SuppressWarnings("rawtypes")
520
    protected String compoundSelect(FeatureType type, Evaluator evaluator,
521
        FeatureQueryOrder featureQueryOrder) {
522

  
523
        StringBuilder query = new StringBuilder();
524
        query.append("SELECT ");
525
        FeatureAttributeDescriptor[] attributeDescriptors = type.getAttributeDescriptors();
526
        for (int i = 0; i < attributeDescriptors.length; i++) {
527
            query.append(attributeDescriptors[i].getName());
528
            // Don't add the last comma
529
            if (i < attributeDescriptors.length - 1) {
530
                query.append(",");
531
            }
532
        }
533

  
534
        query.append(" FROM ");
535
        query.append("'");
536
        query.append(getOGRParameters().getLayerName());
537
        query.append("'");
538

  
539
        if (featureQueryOrder != null && featureQueryOrder.iterator().hasNext()) {
540
            query.append(" ORDER BY ");
541
            Iterator iterator = featureQueryOrder.iterator();
542
            while (iterator.hasNext()) {
543
                FeatureQueryOrderMember member = (FeatureQueryOrderMember) iterator.next();
544

  
545
                if (member.hasEvaluator()) {
546
                    // TODO
547
                } else {
548
                    query.append(member.getAttributeName());
549
                }
550
                if (member.getAscending()) {
551
                    query.append(" ASC");
552
                } else {
553
                    query.append(" DESC");
554
                }
555
                if (iterator.hasNext()) {
556
                    query.append(", ");
557
                } else {
558
                    query.append(' ');
559
                    break;
560
                }
561
            }
562
        }
563

  
564
        return query.toString();
565
    }
566

  
567
    @Override
568
    protected void doDispose() throws BaseException {
569
        super.doDispose();
570
        getResource().removeConsumer(this);
571
        this.resourceProvider = null;
572
        getDataSource().delete();
573
        this.envelope = null;
574
        this.newLayer = null;
575
        this.dataSource = null;
576
        this.opened = false;
577
        this.updateSupport = null;
578
    }
579

  
580
    @Override
581
    public boolean closeResourceRequested(ResourceProvider resource) {
582

  
583
        try {
584
            getDataSource().delete();
585
        } catch (OGRUnsupportedFormatException e) {
586
            LOG.warn(String.format("Can not close resource requested %1s", resource), e);
587
        }
588
        this.envelope = null;
589
        this.newLayer = null;
590
        this.dataSource = null;
591
        this.opened = false;
592
        this.updateSupport = null;
593
        return true;
594
    }
595

  
596
    @Override
597
    public void resourceChanged(ResourceProvider resource) {
598

  
599
        try {
600
            getDataSource().delete();
601
        } catch (OGRUnsupportedFormatException e) {
602
            LOG.warn(String.format("Can not close resource requested %1s", resource), e);
603
        }
604
        this.envelope = null;
605
        this.newLayer = null;
606
        this.dataSource = null;
607
        this.opened = false;
608
        this.updateSupport = null;
609
    }
610
}
tags/org.gvsig.gdal-1.0.20/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRFetureSetProvider.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.regex.Matcher;
27
import java.util.regex.Pattern;
28

  
29
import org.cresques.cts.ICRSFactory;
30
import org.cresques.cts.IProjection;
31
import org.gdal.ogr.DataSource;
32
import org.gdal.ogr.Feature;
33
import org.gdal.ogr.Geometry;
34
import org.gdal.ogr.Layer;
35
import org.gdal.ogr.ogr;
36
import org.gdal.ogr.ogrConstants;
37
import org.gdal.osr.SpatialReference;
38
import org.gvsig.fmap.crs.CRSFactory;
39
import org.gvsig.fmap.dal.DataStore;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.feature.FeatureQuery;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
44
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
45
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider;
46
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
47
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
48
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
49
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
50
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
51
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.exception.BaseException;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

  
57
/**
58
 * 
59
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
60
 *
61
 */
62
public class OGRFetureSetProvider extends AbstractFeatureSetProvider implements FeatureSetProvider,
63
    ResourceConsumer {
64

  
65
    private static final Logger LOG = LoggerFactory.getLogger(OGRFetureSetProvider.class);
66

  
67
    private static final String regularExpresion =
68
        "\\s*ST_intersects[(]\\s*ST_GeomFromText[(]\\s*'([^']*)'\\s*,\\s*'([^']*)'\\s*[)],\\s*(?s).*";
69

  
70
    /**
71
     * @param store
72
     *            Source of this feature set
73
     * @param query
74
     *            Query
75
     * @param featureType
76
     *            Featuretype requested
77
     */
78
    public OGRFetureSetProvider(AbstractFeatureStoreProvider store, FeatureQuery query,
79
        FeatureType featureType) {
80
        super(store, query, featureType);
81

  
82
        getStore().getResource().addConsumer(this);
83
    }
84

  
85
    @Override
86
    public boolean canFilter() {
87
        return false;
88
    }
89

  
90
    @Override
91
    public boolean canOrder() {
92
        return true;
93
    }
94

  
95
    @Override
96
    public boolean canIterateFromIndex() {
97
        return true;
98
    }
99

  
100
    @Override
101
    public long getSize() throws DataException {
102
        return getStore().getFeatureCount();
103
    }
104

  
105
    @Override
106
    public boolean isEmpty() throws DataException {
107
        return getSize() == 0;
108
    }
109

  
110
    @Override
111
    protected AbstractFeatureProviderIterator createIterator(long index) throws DataException {
112
        return createFastIterator(index);
113
    }
114

  
115
    @Override
116
    protected AbstractFeatureProviderIterator createFastIterator(long index) throws DataException {
117
        String sql =
118
            getOGRStoreProvider().compoundSelect(getFeatureType(), getQuery().getFilter(),
119
                getQuery().getOrder());
120

  
121
        DataSource dataSource = getOGRStoreProvider().getDataSource();
122
        LOG.debug("Running query {} over {}", new Object[] { sql, dataSource.getName() });
123

  
124
        Layer layer = null;
125
        if (getOGRStoreProvider().getLayer().TestCapability(ogrConstants.OLCFastSpatialFilter)
126
            && getQuery().hasFilter()) {
127

  
128
            String evaluatorSQL = getQuery().getFilter().getSQL();
129
            Pattern pattern = Pattern.compile(regularExpresion);
130
            Matcher matcher = pattern.matcher(evaluatorSQL);
131

  
132
            if (matcher.matches()) {
133
                String wktGeometry = matcher.group(1);
134
                String crsCode = matcher.group(2);
135

  
136
                IProjection projection = CRSFactory.getCRS(crsCode);
137
                SpatialReference srs =
138
                    new SpatialReference(projection.export(ICRSFactory.FORMAT_WKT));
139
                Geometry ogrGeometry = ogr.CreateGeometryFromWkt(wktGeometry, srs);
140

  
141
                if (getOGRStoreProvider().getDynValue(DataStore.METADATA_CRS) != null) {
142

  
143
                    IProjection projectionProvider =
144
                        (IProjection) getOGRStoreProvider().getDynValue(DataStore.METADATA_CRS);
145
                    if (!crsCode.equals(projectionProvider.getAbrev())) {
146
                        SpatialReference srsDst =
147
                            new SpatialReference(projectionProvider.export(ICRSFactory.FORMAT_WKT));
148
                        ogrGeometry.TransformTo(srsDst);
149
                    }
150
                }
151
                layer = dataSource.ExecuteSQL(sql, ogrGeometry);
152
            }
153
        } else {
154
            layer = dataSource.ExecuteSQL(sql);
155
        }
156
        
157
        if(layer == null){
158
            LOG.warn("The result of query is null. SQL {} FeatureType {}", sql, getFeatureType());
159
            LOG.warn("Getting data without SQL filter");
160
            OGRDataStoreParameters ogrParameters = (OGRDataStoreParameters) getOGRStoreProvider().getParameters();
161
            layer = dataSource.ExecuteSQL("SELECT * FROM ".concat(ogrParameters.getLayerName()));
162
        }
163
        
164
        return new OGRFastIterator(getStore(), layer, index);
165
    }
166

  
167
    @Override
168
    protected void doDispose() throws BaseException {
169
        getStore().getResource().removeConsumer(this);
170
    }
171

  
172
    @Override
173
    public boolean closeResourceRequested(ResourceProvider resource) {
174
        return true;
175
    }
176

  
177
    @Override
178
    public void resourceChanged(ResourceProvider resource) {
179
    }
180

  
181
    class OGRFastIterator extends AbstractFeatureProviderIterator implements ResourceConsumer {
182

  
183
        private long index;
184

  
185
        private Layer layer;
186

  
187
        private OGRConverter converter;
188

  
189
        private FeatureProvider featureProvider;
190

  
191
        public OGRFastIterator(FeatureStoreProvider storeProvider, Layer layer, long index)
192
            throws OGRUnsupportedFormatException {
193
            super(storeProvider);
194
            ToolsLocator.getDisposableManager().bind(storeProvider);
195
            getResource().addConsumer(this);
196
            this.converter = new OGRConverter();
197
            this.index = index;
198
            this.layer = layer;
199
            this.layer.SetNextByIndex((int) index);
200
        }
201

  
202
        @Override
203
        public void remove() {
204
            Feature ogrFeature;
205
            ogrFeature = this.layer.GetNextFeature();
206
            this.layer.DeleteFeature(ogrFeature.GetFID());
207
            index++;
208
        }
209

  
210
        @Override
211
        protected Object internalNext() {
212
            Feature ogrFeature = this.layer.GetNextFeature();
213
            this.featureProvider = converter.convert(featureProvider, getFeatureType(), ogrFeature);
214
            index++;
215
            return this.featureProvider;
216
        }
217

  
218
        @Override
219
        protected boolean internalHasNext() {
220
            return index < this.layer.GetFeatureCount() ? true : false;
221
        }
222

  
223
        @Override
224
        protected void doDispose() throws BaseException {
225
            ToolsLocator.getDisposableManager().release(getStore());
226
            getOGRStoreProvider().getDataSource().ReleaseResultSet(layer);
227
            getResource().removeConsumer(this);
228
            this.converter = null;
229
            this.featureProvider = null;
230
            this.layer = null;
231
        }
232

  
233
        @Override
234
        public boolean closeResourceRequested(ResourceProvider resource) {
235
            this.featureProvider = null;
236
            return true;
237
        }
238

  
239
        @Override
240
        public void resourceChanged(ResourceProvider resource) {
241
            throw new ConcurrentDataModificationException(getOGRStoreProvider().getName());
242
        }
243
    }
244

  
245
    private OGRDataStoreProvider getOGRStoreProvider() {
246
        return (OGRDataStoreProvider) getStore();
247
    }
248
}
tags/org.gvsig.gdal-1.0.20/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRLibrary.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.apache.commons.lang3.StringUtils;
27
import org.gdal.ogr.DataSource;
28
import org.gdal.ogr.FeatureDefn;
29
import org.gdal.ogr.GeomFieldDefn;
30
import org.gdal.ogr.Layer;
31
import org.gdal.ogr.ogr;
32
import org.gvsig.fmap.dal.DALFileLibrary;
33
import org.gvsig.fmap.dal.DALFileLocator;
34
import org.gvsig.fmap.dal.DALLibrary;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.FileHelper;
37
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices;
38
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
39
import org.gvsig.metadata.MetadataLibrary;
40
import org.gvsig.metadata.exceptions.MetadataException;
41
import org.gvsig.tools.ToolsLocator;
42
import org.gvsig.tools.dynobject.DynClass;
43
import org.gvsig.tools.dynobject.DynField;
44
import org.gvsig.tools.dynobject.DynField_v2;
45
import org.gvsig.tools.dynobject.DynField_v2.AvailableValuesProvider;
46
import org.gvsig.tools.dynobject.DynObject;
47
import org.gvsig.tools.dynobject.DynObjectManager;
48
import org.gvsig.tools.dynobject.DynObjectValueItem;
49
import org.gvsig.tools.library.AbstractLibrary;
50
import org.gvsig.tools.library.LibraryException;
51
import org.gvsig.tools.persistence.PersistenceManager;
52

  
53
/**
54
 * 
55
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
56
 *
57
 */
58
public class OGRLibrary extends AbstractLibrary {
59

  
60
    @Override
61
    public void doRegistration() {
62
        registerAsImplementationOf(OGRLibrary.class);
63
        require(DALLibrary.class);
64
        require(DALFileLibrary.class);
65
        require(MetadataLibrary.class);
66
    }
67

  
68
    @Override
69
    protected void doInitialize() throws LibraryException {
70

  
71
        // Register all drivers
72
        ogr.RegisterAll();
73
    }
74

  
75
    @Override
76
    protected void doPostInitialize() throws LibraryException {
77

  
78
        // Register OGR parameters definition
79
        FileHelper.registerParametersDefinition(OGRDataStoreParameters.PARAMETERS_DEFINITION_NAME,
80
            OGRDataStoreParameters.class, "OGRDataStoreParameters.xml");
81

  
82
        FileHelper.registerParametersDefinition(
83
            OGRNewDataStoreParameters.PARAMETERS_DEFINITION_NAME, OGRNewDataStoreParameters.class,
84
            "OGRNewDataStoreParameters.xml");
85

  
86
        FileHelper.registerParametersDefinition(
87
            OGRDataExplorerParameters.PARAMETERS_DEFINITION_NAME, OGRDataExplorerParameters.class,
88
            "OGRExplorerParameters.xml");
89

  
90
        // Register OGR metadata definition
91
        try {
92
            FileHelper.registerMetadataDefinition(OGRDataStoreProvider.METADATA_DEFINITION_NAME,
93
                OGRDataStoreProvider.class, "OGRMetadata.xml");
94
        } catch (MetadataException e) {
95
            throw new LibraryException(getClass(), e);
96
        }
97

  
98
        // Register OGR provider factory
99
        DataManagerProviderServices dataman =
100
            (DataManagerProviderServices) DALLocator.getDataManager();
101
        if (!dataman.getStoreProviders().contains(OGRDataStoreProvider.NAME)) {
102
            dataman.registerStoreProviderFactory(new BasicOGRDataStoreProviderFactory(
103
                OGRDataStoreProvider.NAME, OGRDataStoreProvider.DESCRIPTION));
104
        }
105

  
106
        // Register OGR data explorer provider
107
        if (!dataman.getExplorerProviders().contains(OGRDataExplorer.NAME)) {
108
            dataman.registerExplorerProvider(OGRDataExplorer.NAME, OGRDataExplorer.class,
109
                OGRDataExplorerParameters.class);
110
        }
111

  
112
        // Register OGR file system server provider
113
        DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
114
            OGRDataStoreProvider.NAME, OGRDataStoreProvider.DESCRIPTION,
115
            OGRFilesystemServerProvider.class);
116

  
117
        DynObjectManager dynMan = ToolsLocator.getDynObjectManager();
118
        DynClass dynClass = dynMan.get(OGRDataStoreProvider.METADATA_DEFINITION_NAME);
119
        if (dynClass == null) {
120
            dynClass = dynMan.add(OGRDataStoreProvider.METADATA_DEFINITION_NAME);
121

  
122
        }
123

  
124
        // Register OGR resource provider
125
        ResourceManagerProviderServices resman =
126
            (ResourceManagerProviderServices) DALLocator.getResourceManager();
127
        if (!resman.getResourceProviders().contains(OGRResource.NAME)) {
128
            resman.register(OGRResource.NAME, OGRResource.DESCRIPTION, OGRResource.class,
129
                OGRResourceParameters.class);
130
        }
131

  
132
        // Register persistence definition of OGR resource parameters
133
        OGRResourceParameters.registerPersistenceDefinition();
134

  
135
        fixDataParameters();
136
    }
137

  
138
    private void fixDataParameters() {
139
        
140
        // XXX: This code is commented because until version of gvSIG tools at
141
        // gvSIG Desktop POM is greater or equal to 3.0.78. If the version is
142
        // greater or equals remove this comment and uncomment code.
143
        
144
        
145
        // DynObjectManager dynObjectManager =
146
        // ToolsLocator.getDynObjectManager();
147
        // DynClass dynClass =
148
        // dynObjectManager.get(PersistenceManager.PERSISTENCE_NAMESPACE,
149
        // OGRDataStoreParameters.PARAMETERS_DEFINITION_NAME);
150
        // DynField_v2 dynField = (DynField_v2)
151
        // dynClass.getDynField("defaultGeometryField");
152
        // dynField.setAvailableValuesProvider(new AvailableValuesProvider() {
153
        //
154
        // @Override
155
        // public DynObjectValueItem[] getValues(DynField field, DynObject
156
        // dynObject) {
157
        //
158
        // if (dynObject instanceof OGRDataStoreParameters) {
159
        //
160
        // DataSource dataSource = null;
161
        // try {
162
        // // Open connection string
163
        // OGRDataStoreParameters ogrParameters = (OGRDataStoreParameters)
164
        // dynObject;
165
        // if (StringUtils.isNotBlank(ogrParameters.getConnectionString())) {
166
        // dataSource = ogr.Open(ogrParameters.getConnectionString());
167
        // } else if (ogrParameters.getFile() != null
168
        // && ogrParameters.getFile().exists()) {
169
        // dataSource = ogr.Open(ogrParameters.getFile().getAbsolutePath());
170
        // }
171
        //
172
        // // Get layer
173
        // Layer layer = null;
174
        // if (StringUtils.isNotBlank(ogrParameters.getLayerName())) {
175
        // layer = dataSource.GetLayerByName(ogrParameters.getLayerName());
176
        // }
177
        //
178
        // // Get geometry fields
179
        // if (layer != null) {
180
        // FeatureDefn layerDefn = layer.GetLayerDefn();
181
        // DynObjectValueItem[] valueItems =
182
        // new DynObjectValueItem[layerDefn.GetGeomFieldCount()];
183
        // for (int i = 0; i < layerDefn.GetGeomFieldCount(); i++) {
184
        // GeomFieldDefn geomFieldDefn = layerDefn.GetGeomFieldDefn(i);
185
        // valueItems[i] = new DynObjectValueItem(geomFieldDefn.GetName());
186
        // }
187
        //
188
        // return valueItems;
189
        // }
190
        // } finally {
191
        // // Free native resources
192
        // dataSource.delete();
193
        // }
194
        // }
195
        // return null;
196
        // }
197
        //
198
        // });
199
    }
200
}
tags/org.gvsig.gdal-1.0.20/org.gvsig.gdal.prov/org.gvsig.gdal.prov.ogr/src/main/java/org/gvsig/gdal/prov/ogr/OGRNewDataStoreParameters.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.List;
27
import java.util.Vector;
28

  
29
import org.gvsig.fmap.dal.feature.EditableFeatureType;
30
import org.gvsig.fmap.dal.feature.FeatureType;
31
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
32
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
33
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.dynobject.DelegatedDynObject;
35

  
36
/**
37
 * 
38
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
39
 *
40
 */
41
public class OGRNewDataStoreParameters extends OGRDataStoreParameters implements
42
    NewFeatureStoreParameters {
43

  
44
    /**
45
     * 
46
     */
47
    public static final String PARAMETERS_DEFINITION_NAME = "OGRNewDataStoreParameters";
48

  
49
    /**
50
     * 
51
     */
52
    public static final String DRIVER_NAME_PARAMETER_NAME = "driverName";
53

  
54
    /**
55
     * 
56
     */
57
    public static final String DATASET_CREATION_OPTION_PARAMETER_NAME = "datasetCreationOptions";
58

  
59
    /**
60
     * 
61
     */
62
    public OGRNewDataStoreParameters() {
63
        parameters =
64
            (DelegatedDynObject) ToolsLocator.getDynObjectManager().createDynObject(
65
                ToolsLocator.getPersistenceManager().getDefinition(PARAMETERS_DEFINITION_NAME));
66
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME,
67
            OGRDataStoreProvider.NAME);
68
    }
69

  
70
    @Override
71
    public void setDefaultFeatureType(FeatureType defaultFeatureType) {
72
        this.setDynValue(FEATURETYPE_PARAMTER_NAME, defaultFeatureType);
73
    }
74

  
75
    @Override
76
    public EditableFeatureType getDefaultFeatureType() {
77
        return (EditableFeatureType) this.getDynValue(FEATURETYPE_PARAMTER_NAME);
78
    }
79

  
80
    /**
81
     * @param driverName
82
     *            Sets driver that will be used to create new stores
83
     */
84
    public void setDriverName(String driverName) {
85
        this.setDynValue(DRIVER_NAME_PARAMETER_NAME, driverName);
86
    }
87

  
88
    /**
89
     * @return Name of driver to create new data stores
90
     */
91
    public String getDriverName() {
92
        return (String) this.getDynValue(DRIVER_NAME_PARAMETER_NAME);
93
    }
94

  
95
    /**
96
     * 
97
     * @param options
98
     *            Set dataset creation options
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff