Revision 294

View differences:

org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.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.lidar</artifactId>
6
		<version>1.0.1-SNAPSHOT</version>
7
	</parent>
8
	<artifactId>org.gvsig.lidar.prov</artifactId>
9
	<packaging>pom</packaging>
10
	<name>org.gvsig.lidar.prov</name>
11
	<modules>
12
		<module>org.gvsig.lidar.prov.common</module>
13
		<module>org.gvsig.lidar.prov.whitebox</module>
14
		<module>org.gvsig.lidar.prov.jgrass</module>
15
	</modules>
16
</project>
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/src/main/resources/org/gvsig/lidar/prov/LASDataStoreParameters.xml
1
<?xml version="1.0"?>
2
<definitions>
3
	<version>1.0.0</version>
4
	<classes>
5
		<class name="LASDataStoreParameters">
6
			<extends>
7
				<class namespace="dal" name="ProviderParameters" />
8
			</extends>
9
			<description>Parameters needed to open LAS stores
10
			</description>
11
			<fields>
12
				<field name="CRS" type="crs" mandatory="true" group="Basic">
13
					<description>Coordinate reference system used</description>
14
				</field>
15
				<field name="file" type="file" mandatory="true" group="Basic">
16
					<description>LAS file</description>
17
				</field>
18
				<field name="thinningDivisor" type="integer" mandatory="false" group="Basic">
19
					<description>Loads only 1 of every n points (1/n), e.g. thinningDivisor=10 loads 10% of total points</description>
20
				</field>
21
				<field name="thinningResolution" type="double" mandatory="false" group="Advanced">
22
					<description>Loads n points per square map unit (e.g. 0.0001 points/m2 on a projected layer or 100 points/km2)</description>
23
				</field>
24
			</fields>
25
		</class>
26
	</classes>
27
</definitions>  
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/src/main/java/org/gvsig/lidar/prov/LASUnsupportedFormatException.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.lidar.prov;
25

  
26
import java.util.Map;
27

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

  
30
/**
31
 * 
32
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
33
 *
34
 */
35
@SuppressWarnings("unchecked")
36
public class LASUnsupportedFormatException extends DataException {
37

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

  
40
    private final static String MESSAGE =
41
        "Unsupported vectorial format. There is no LAS 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 LASUnsupportedFormatException(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 LASUnsupportedFormatException(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 LASUnsupportedFormatException(String connectionString) {
82
        super(MESSAGE, KEY, serialVersionUID);
83
        this.values.put("connectionString", connectionString);
84
    }
85

  
86
}
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/src/main/java/org/gvsig/lidar/prov/LASDataStoreProvider.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.lidar.prov;
25

  
26
import java.io.File;
27
import java.util.Iterator;
28

  
29
import org.apache.commons.io.FileUtils;
30
import org.gvsig.fmap.dal.DataStore;
31
import org.gvsig.fmap.dal.DataStoreParameters;
32
import org.gvsig.fmap.dal.DataTypes;
33
import org.gvsig.fmap.dal.exception.CloseException;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.exception.InitializeException;
36
import org.gvsig.fmap.dal.exception.OpenException;
37
import org.gvsig.fmap.dal.exception.ReadRuntimeException;
38
import org.gvsig.fmap.dal.feature.FeatureQuery;
39
import org.gvsig.fmap.dal.feature.FeatureSet;
40
import org.gvsig.fmap.dal.feature.FeatureStore;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
43
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
44
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
45
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
46
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
47
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
48
import org.gvsig.fmap.dal.resource.ResourceAction;
49
import org.gvsig.fmap.dal.resource.exception.ResourceException;
50
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
51
import org.gvsig.fmap.dal.resource.file.FileResource;
52
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
53
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
54
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
55
import org.gvsig.fmap.geom.GeometryLocator;
56
import org.gvsig.fmap.geom.GeometryManager;
57
import org.gvsig.fmap.geom.primitive.Envelope;
58
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
59
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
60
import org.gvsig.tools.dispose.DisposableIterator;
61
import org.gvsig.tools.dynobject.DynObject;
62
import org.gvsig.tools.exception.BaseException;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65

  
66
/**
67
 * 
68
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
69
 *
70
 */
71
public abstract class LASDataStoreProvider extends AbstractFeatureStoreProvider implements
72
    FeatureStoreProvider, ResourceConsumer {
73

  
74
    private static final Logger LOG = LoggerFactory.getLogger(LASDataStoreProvider.class);
75
    
76
    public static final String GEOM_FIELD="GEOM";
77
    public static final String CLASS_FIELD="CLASSIFICATION";
78
    public static final String POINTX_FIELD="POINTX";
79
    public static final String POINTY_FIELD="POINTY";
80
    public static final String POINTZ_FIELD="POINTZ";
81
    public static final String TIME_FIELD="TIME";
82
    public static final String WEEKTIME_FIELD="WEEKTIME";
83
    public static final String COLOR_FIELD="COLOR";
84
    public static final String INTENSITY_FIELD="INTENSITY";
85
    public static final String RETURNNUM_FIELD="RETURNNUM";
86
    public static final String NUMRETURNS_FIELD="NUMRETURNS";
87

  
88
    protected File file;
89

  
90
    protected Envelope envelope = null;
91

  
92
    protected ResourceProvider resourceProvider;
93

  
94
    protected boolean opened = false;
95
    protected long counterNewsOIDs = -1;
96
    
97
    protected  GeometryManager gm;
98
    
99
    protected double decimation = 1.0d;
100
    protected double resolution = 0.0d;
101
 
102
    protected LASDataStoreProvider(DataStoreParameters dataParameters,
103
        DataStoreProviderServices storeServices, DynObject metadata) throws InitializeException {
104
        super(dataParameters, storeServices, metadata);
105

  
106
        // Set CRS parameter to metadata
107
        this.setDynValue(DataStore.METADATA_CRS, dataParameters.getDynValue(DataStore.METADATA_CRS));
108
        file = (File) dataParameters.getDynValue(LASDataStoreParameters.FILE_PARAMETER_NAME);
109
        
110
        if (dataParameters instanceof LASDataStoreParameters) {
111
        	LASDataStoreParameters lasParameters = (LASDataStoreParameters) dataParameters;
112
        	decimation = (double) lasParameters.getDecimation();
113
        	if (decimation<1) {
114
        		decimation = 1;
115
        	}
116
        	resolution = lasParameters.getResolution();
117
        }
118

  
119
        getResource().addConsumer(this);
120
        this.gm = GeometryLocator.getGeometryManager();
121
        try {
122
            this.open();
123
        } catch (OpenException e) {
124
            throw new InitializeException(getProviderName(), e);
125
        }
126
    }
127
    
128
    /*
129
    public FeatureProvider createFeatureProvider(FeatureType type) throws DataException {
130
        return new DBFFeatureProvider(this, type);
131
    }*/
132

  
133
    @Override
134
    public String getFullName() {
135
    	return getName();
136
    }
137

  
138
    @Override
139
    public String getName() {
140
        return getLASParameters().getFile().getName();
141
    }
142

  
143
    @Override
144
    public boolean allowWrite() {
145
    	return false;
146
    }
147

  
148
    @Override
149
    public ResourceProvider getResource() {
150
        if (this.resourceProvider == null) {
151
            try {
152
                this.resourceProvider =
153
                    this.createResource(FileResource.NAME, new Object[] { getLASParameters()
154
                        .getFile().getAbsolutePath() });
155
            } catch (InitializeException e) {
156
                throw new ReadRuntimeException(String.format(
157
                    "Can not create file resource with %1s path", getLASParameters().getFile()
158
                        .getAbsolutePath()), e);
159
            }
160
        }
161

  
162
        return resourceProvider;
163
    }
164

  
165
    @Override
166
    public Object getSourceId() {
167
        return this.getLASParameters().getFile();
168
    }
169

  
170
    @Override
171
    public void open() throws OpenException {
172
    	// FIXME: should this method be synchronized?
173
        if (opened == false) {
174
            try {
175
            	this.opened = loadFeatureType();
176
                updateDecimationFromResolution();
177
            } catch (BaseException e) {
178
                LOG.error("Can not load feature type", e);
179
                throw new OpenException(getFullName(), e);
180
            }
181
        }
182
    }
183
    
184
    protected void updateDecimationFromResolution() {
185
    	if (resolution>0.0d) {
186
    		try {
187
				double width = getEnvelope().getLength(0);
188
				double height = getEnvelope().getLength(1);
189
				long count = getFeatureCount();
190
				double origResolution = ((count/width)/height); // divide twice instead of multiply denominator to avoid overflow of the product
191
				double calculatedDecimation = origResolution/resolution;
192
				if (calculatedDecimation>1) {
193
					decimation = calculatedDecimation;
194
				}
195
				else {
196
					decimation = 1;
197
				}
198
			} catch (DataException e) {
199
				// TODO Auto-generated catch block
200
				e.printStackTrace();
201
			}
202
    		
203
    	}
204
    		
205
    }
206

  
207
    protected abstract boolean loadFeatureType() throws LASUnsupportedFormatException,
208
        GeometryTypeNotSupportedException, GeometryTypeNotValidException, DataException;
209
    
210
    
211
    @SuppressWarnings("rawtypes")
212
    @Override
213
    public void performChanges(Iterator deleteds, Iterator inserteds,
214
            Iterator updateds, Iterator originalFeatureTypesUpdated)
215
            throws PerformEditingException {
216

  
217
        try {
218
            final FeatureStore store
219
                    = this.getStoreServices().getFeatureStore();
220
            getResource().closeRequest();
221
            getResource().execute(new ResourceAction() {
222

  
223
                public Object run() throws Exception {
224
                    FeatureSet set = null;
225
                    DisposableIterator iter = null;
226
                    try {
227
                        set = store.getFeatureSet();
228
                        LASDataStoreParameters params = (LASDataStoreParameters) getParameters();
229
                        LASDataStoreParameters tmpParams
230
                                = (LASDataStoreParameters) params.getCopy();
231

  
232
                        File tmp_file = File.createTempFile(
233
                                "tmp_" + System.currentTimeMillis(), ".las");
234
                        tmp_file.deleteOnExit();
235

  
236
                        tmpParams.setFile(tmp_file);
237
                        /*
238
                        writer.begin(tmpParams, store.getDefaultFeatureType(),
239
                                set.getSize());
240

  
241
                        iter = set.fastIterator();
242
                        while (iter.hasNext()) {
243
                            Feature feature = (Feature) iter.next();
244
                            writer.append(feature);
245
                        }
246

  
247
                        writer.end();
248
						*/
249
                        try {
250
                            close();
251
                        } catch (CloseException e1) {
252
                            throw new PerformEditingException(getProviderName(), e1);
253
                        }
254
                        params.getFile().delete();
255

  
256
                        File target_file = params.getFile();
257
                        if (target_file.exists()) {
258
                            target_file.delete();
259
                        }
260
                        tmp_file.renameTo(target_file);
261

  
262
                        if (tmp_file.exists() && !target_file.exists()) {
263
                            // Renaming failed, let's simply copy.
264
                            // We assume we cannot delete it, but we
265
                            // used deleteOnExit and it's
266
                            // temporary, so no problem
267
                            LOG.info("Warning: copying tmp file instead of renaming: "
268
                                    + target_file.getName());
269
                            FileUtils.copyFile(tmp_file, target_file);
270
                        }
271
                        
272
                        getResource().notifyChanges();
273
                        loadFeatureType();
274
                    } finally {
275
                        if (set != null) {
276
                            set.dispose();
277
                        }
278
                        if (iter != null) {
279
                            iter.dispose();
280
                        }
281
                    }
282
                    return null;
283
                }
284
            });
285
        } catch (ResourceExecuteException | ResourceException e) {
286
            throw new PerformEditingException(this.getProviderName(), e);
287
        }
288

  
289
        this.counterNewsOIDs = -1;
290
    }
291

  
292
    @Override
293
    public Object createNewOID() {
294
        if (this.counterNewsOIDs < 0) {
295
            try {
296
                this.counterNewsOIDs = this.getFeatureCount();
297
            } catch (DataException e) {
298
                e.printStackTrace();
299
            }
300

  
301
        } else {
302
            this.counterNewsOIDs++;
303
        }
304
        return new Long(counterNewsOIDs);
305
    }
306

  
307
    @Override
308
    public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType)
309
        throws DataException {
310
        open();
311
        return new LASFetureSetProvider(this, query, featureType);
312
    }
313

  
314
    @Override
315
    public int getOIDType() {
316
        return DataTypes.LONG;
317
    }
318

  
319
    @Override
320
    protected FeatureProvider internalGetFeatureProviderByReference(
321
        FeatureReferenceProviderServices reference, FeatureType featureType)
322
        throws DataException {
323

  
324
    	return internalGetFeatureProviderByIndex(((Long) reference.getOID()).longValue(), featureType);
325
    }
326
    
327
    public abstract FeatureProvider internalGetFeatureProviderByIndex(
328
    		long index, FeatureType featureType)
329
    				throws DataException;
330

  
331
    protected LASDataStoreParameters getLASParameters() {
332
        return (LASDataStoreParameters) this.getParameters();
333
    }
334
}
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/src/main/java/org/gvsig/lidar/prov/LASFetureSetProvider.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.lidar.prov;
25

  
26
import org.gvsig.fmap.dal.exception.DataException;
27
import org.gvsig.fmap.dal.feature.FeatureQuery;
28
import org.gvsig.fmap.dal.feature.FeatureType;
29
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureProviderIterator;
30
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureSetProvider;
31
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider;
32
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
33
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.exception.BaseException;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

  
39
/**
40
 * 
41
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
42
 *
43
 */
44
public class LASFetureSetProvider extends AbstractFeatureSetProvider implements FeatureSetProvider {
45

  
46
    private static final Logger LOG = LoggerFactory.getLogger(LASFetureSetProvider.class);
47

  
48
    private static final String regularExpresion =
49
        "\\s*ST_intersects[(]\\s*ST_GeomFromText[(]\\s*'([^']*)'\\s*,\\s*'([^']*)'\\s*[)],\\s*(?s).*";
50

  
51
    /**
52
     * @param store
53
     *            Source of this feature set
54
     * @param query
55
     *            Query
56
     * @param featureType
57
     *            Featuretype requested
58
     */
59
    public LASFetureSetProvider(AbstractFeatureStoreProvider store, FeatureQuery query,
60
        FeatureType featureType) {
61
        super(store, query, featureType);
62
    }
63

  
64
    @Override
65
    public boolean canFilter() {
66
        return false;
67
    }
68

  
69
    @Override
70
    public boolean canOrder() {
71
        return false;
72
    }
73

  
74
    @Override
75
    public boolean canIterateFromIndex() {
76
        return true;
77
    }
78

  
79
    @Override
80
    public long getSize() throws DataException {
81
        return getStore().getFeatureCount(); // FIXME
82
    }
83

  
84
    @Override
85
    public boolean isEmpty() throws DataException {
86
        return getSize() == 0;
87
    }
88

  
89
    @Override
90
    protected AbstractFeatureProviderIterator createIterator(long index) throws DataException {
91
        return createFastIterator(index);
92
    }
93

  
94
    @Override
95
    protected AbstractFeatureProviderIterator createFastIterator(long index) throws DataException {
96
        return new LASFastIterator(getStore(), index);
97
    }
98

  
99
    @Override
100
    protected void doDispose() throws BaseException {
101
    }
102

  
103
    class LASFastIterator extends AbstractFeatureProviderIterator {
104

  
105
        private long index;
106

  
107
        public LASFastIterator(FeatureStoreProvider storeProvider, long index)
108
            throws LASUnsupportedFormatException {
109
            super(storeProvider);
110
            ToolsLocator.getDisposableManager().bind(storeProvider);
111
            this.index = index;
112
        }
113

  
114
        @Override
115
        public void remove() {
116
        	throw new UnsupportedOperationException();
117
        }
118

  
119
        @Override
120
        protected Object internalNext() {
121
        	
122
        	try {
123
				return ((LASDataStoreProvider)this.getDataStoreProvider()).internalGetFeatureProviderByIndex(index++, getFeatureType());
124
			} catch (DataException e) {
125
				// TODO Auto-generated catch block
126
				e.printStackTrace();
127
			}
128
        	return null;
129
        }
130

  
131
        @Override
132
        protected boolean internalHasNext() {
133
            try {
134
				return index < ((LASDataStoreProvider)this.getDataStoreProvider()).getFeatureCount() ? true : false;
135
			} catch (DataException e) {
136
				e.printStackTrace();
137
			}
138
            return false;
139
        }
140

  
141
        @Override
142
        protected void doDispose() throws BaseException {
143
            ToolsLocator.getDisposableManager().release(getStore());
144
        }
145
    }
146
}
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/src/main/java/org/gvsig/lidar/prov/ConversionUtils.java
1
package org.gvsig.lidar.prov;
2

  
3
public class ConversionUtils {
4
    
5
    private static final int RED_MASK = 255 << 16;
6
    private static final int GREEN_MASK = 255 << 8;
7
    private static final int BLUE_MASK = 255;
8

  
9
    /**
10
     * GPS time as GPS timestamp = LAS  GPS adjusted time + 1000000000
11
     * GPS time as Unix timestamp = GPS time as GPS timestamp + 315964800
12
     * UTC time as Unix timestamp = GPS time as Unix timestamp + leap seconds
13
     * As leap seconds depend on the actual date being converted, we'll ignore them
14
     * Therefore, we'll use the following constant
15
     * UNIX TIME = LAS GPS adjusted time + LAS_TIME_TO_UNIX_TIME,
16
     * where LAS_TIME_TO_UNIX_TIME = 1000000000 + 315964800 = 1315964800
17
     */
18
    final public static long LAS_TIME_TO_UNIX_TIME = 1315964800;
19
    
20
    /**
21
     * Converts red, green and blue values to a RGB int as defined
22
     * by java.awt.Color(int) constructor.
23
     * 
24
     */
25
    public static int rgbToInt(int red, int green, int blue) {
26
    	return (red << 16) | (green << 8) | blue;
27
    }
28
    
29
    /**
30
     * Converts a RGB int as defined by java.awt.Color(int) constructor
31
     * to separate red, green and blue values
32
     * 
33
     * @returns an array containing the red, green and blue values
34
     */
35
    public static int[] intToRgb(int rgb) {
36
    	int[] rgbArray = new int[3];
37
    	rgbArray[0] = (rgb & RED_MASK) >> 16;
38
        rgbArray[1] = (rgb & GREEN_MASK) >> 8;
39
    	rgbArray[2] = rgb & BLUE_MASK;
40
    	return rgbArray;
41
    }
42
}
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/src/main/java/org/gvsig/lidar/prov/LASDataStoreProviderFactory.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.lidar.prov;
25

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

  
29
/**
30
 * LAS Data Store Provider factory to create parameters to create
31
 * specific server data explorers and specific data store providers.
32
 * 
33
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
34
 *
35
 */
36
public interface LASDataStoreProviderFactory extends FeatureStoreProviderFactory {
37

  
38
    /**
39
     * Creates specific parameters if factory can open connection object. If it
40
     * can not open, it will be return null.
41
     * 
42
     * @param connectionObject
43
     *            File or connection string to be opened
44
     * @return Data explorer parameters If this factory can open connection
45
     *         object it will be return specific parameters, otherwise return
46
     *         null.
47
     */
48
    public DynObject createParameters(Object connectionObject);
49

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

  
64
}
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/src/main/java/org/gvsig/lidar/prov/LASDataStoreParameters.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.lidar.prov;
25

  
26
import java.io.File;
27
import java.io.IOException;
28

  
29
import org.apache.commons.io.FileUtils;
30
import org.apache.commons.io.FilenameUtils;
31
import org.cresques.cts.ICRSFactory;
32
import org.cresques.cts.IProjection;
33
import org.gvsig.fmap.crs.CRSFactory;
34
import org.gvsig.fmap.dal.DataStoreParameters;
35
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
36
import org.gvsig.fmap.dal.feature.EditableFeatureType;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
40
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
41
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
42
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DelegatedDynObject;
45
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
46

  
47
/**
48
 * 
49
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
50
 *
51
 */
52
public class LASDataStoreParameters extends AbstractDataParameters
53
		implements DataStoreParameters, FilesystemStoreParameters, NewFeatureStoreParameters {
54

  
55
    public static final String FILE_PARAMETER_NAME = "file";
56

  
57
    public static final String CRS_PARAMETER_NAME = "CRS";
58
    
59
    public static final String POINT_DENSITY_PARAMETER_NAME = "PointDensity";
60
    
61
    /**
62
     * Simple method to load a subset of the data, useful for faster
63
     * visualization. The decimation parameter will only keep the n'th
64
     * point from the file. For instance, a value of 2 will skip half
65
     * of the points. A value of 3 will load 1/3 of the original points.
66
     * A value of 10 will load 1/10 of the original points.
67
     */
68
    public static final String THINNING_DIV_PARAMETER_NAME = "thinningDivisor";
69
    
70
    /**
71
     * An alternative method to load a subset of the data. This parameter
72
     * defines how many points should be loaded by square map unit.
73
     * For instance, a value of 0.1 on a layer using EPSG:25830 means that
74
     * 0.1 points should be loaded per square meter (100.000 points per
75
     * square kilometer). The same value for a layer using EPSG:4326 means
76
     * that 0.1 points should be loaded per square degree.
77
     * 
78
     * Note that this is an approximated method which assumes that points are
79
     * evenly separated on x and y axis. When resolution is defined,
80
     * the decimation parameter is ignored.
81
     * 
82
     */
83
    public static final String THINNING_RESOLUTION_PARAMETER_NAME = "thinningResolution";
84
    
85

  
86
    protected DelegatedDynObject parameters;
87

  
88
    public static final String DATASET_CREATION_OPTION_PARAMETER_NAME = "datasetCreationOptions";
89
    
90
    public static final String PARAMETERS_DEFINITION_NAME = "LASDataStoreParameters";
91
    
92
	public LASDataStoreParameters() {
93
		this(PARAMETERS_DEFINITION_NAME, "to be defined");
94
	}
95

  
96
    public LASDataStoreParameters(String providerName) {
97
    	this(PARAMETERS_DEFINITION_NAME, providerName);
98
    }
99
    
100
    public LASDataStoreParameters(String parametersDefinitionName, String providerName) {
101
        this.parameters =
102
            (DelegatedDynObject) ToolsLocator.getDynObjectManager().createDynObject(
103
                ToolsLocator.getPersistenceManager().getDefinition(parametersDefinitionName));
104
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, providerName);
105
    }
106

  
107
	public String getDataStoreName() {
108
		return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
109
	}
110

  
111
	public String getDescription() {
112
		return this.getDynClass().getDescription();
113
	}
114

  
115

  
116

  
117
    /**
118
     * @return File
119
     */
120
    public File getFile() {
121
        return (File) this.getDynValue(FILE_PARAMETER_NAME);
122
    }
123

  
124
    /**
125
     * 
126
     * @return String
127
     */
128
    public IProjection getCRS() {
129
        return (IProjection) this.getDynValue(CRS_PARAMETER_NAME);
130
    }
131

  
132
    /**
133
     * @param file
134
     *            File
135
     */
136
    public void setFile(File file) {
137
        this.setDynValue(FILE_PARAMETER_NAME, file);
138
        if (getCRS()==null) {
139
        	// only set CRS automatically if it has not been previously set
140
        	loadProj();
141
        }
142
    }
143
    
144
    private void loadProj() {
145
    	String wktString = readProj();
146
        if (wktString != null) {
147
            IProjection proj = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT, wktString);
148
            setCRS(proj);
149
        }
150
    }
151
    
152
	private String readProj(){
153
        File prjFile = getPrj();
154
        if (prjFile.exists()) {
155
            try {
156
                return FileUtils.readFileToString(prjFile);
157
            } catch (IOException e) {
158
                return null;
159
            }
160
        }
161
        return null;
162
	}
163
    
164
	/**
165
	 * Gets the .prj path corresponding to the .las file. It will
166
	 * always return a path if getFile is not null, even if none
167
	 * of the files actually exists. 
168
	 *  
169
	 * @return
170
	 */
171
    public File getPrj() {
172
    	File lasFile = getFile(); 
173
    	if (lasFile!=null) {
174
    		String path = lasFile.getPath();
175
    		String baseName = FilenameUtils.getFullPath(path) + FilenameUtils.getBaseName(path);
176
    		return new File(baseName + ".prj");
177
    	}
178
    	return null;
179
    }
180

  
181
    /**
182
     * 
183
     * @param crs
184
     */
185
    public void setCRS(IProjection crs) {
186
        this.setDynValue(CRS_PARAMETER_NAME, crs);
187
    }
188

  
189
    public int getDecimation() {
190
    	try {
191
    		this.hasDynValue(THINNING_DIV_PARAMETER_NAME);
192
    		return (int) this.getDynValue(THINNING_DIV_PARAMETER_NAME);
193
    	}
194
    	catch (Exception ex) {}
195
    	return 0;
196
    }
197
    
198
    public double getResolution() {
199
    	try {
200
    		this.hasDynValue(THINNING_RESOLUTION_PARAMETER_NAME);
201
    		return (double) this.getDynValue(THINNING_RESOLUTION_PARAMETER_NAME);
202
    	}
203
    	catch (Exception ex) {}
204
    	return 0.0d;
205
    }
206
    
207
    @Override
208
    public boolean isValid() {
209
        if (this.getFile() != null && this.getFile().exists()) {
210
            return true;
211
        }
212
        return false;
213
    }
214

  
215
    @Override
216
    protected DelegatedDynObject getDelegatedDynObject() {
217
        return parameters;
218
    }
219

  
220
    @Override
221
    public void setDefaultFeatureType(FeatureType defaultFeatureType) {
222
        this.setDynValue(FEATURETYPE_PARAMTER_NAME, defaultFeatureType);
223
    }
224

  
225
    @Override
226
    public EditableFeatureType getDefaultFeatureType() {
227
        return (EditableFeatureType) this.getDynValue(FEATURETYPE_PARAMTER_NAME);
228
    }
229
    
230
	@Override
231
	public void validate() throws ValidateDataParametersException {
232
		fixParameters();
233
		super.validate();
234
	}
235
    
236
    public void fixParameters() {
237
    	try {
238
    		EditableFeatureType featureType = getDefaultFeatureType();    
239
    		if (featureType != null) {
240
    			FeatureAttributeDescriptor geometryAttribute =
241
    					featureType.getAttributeDescriptor(featureType.getDefaultGeometryAttributeName());
242
    			if (geometryAttribute != null) {
243
    				//If the CRS parameter has not been fixed, but there is
244
    				//a geometric attribute with the CRS, this attribute
245
    				//can be used.
246
    				if (getCRS() == null) {
247
    					setCRS(geometryAttribute.getSRS());
248
    				}
249
    			}
250
    		}
251
    	}
252
    	catch (DynFieldNotFoundException ex) {
253
    		// we are not creating a new NewFeatureStoreParameters,
254
    		// we can safely ignore the error
255
    	}
256
    }
257

  
258
}
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.common/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.lidar.prov</artifactId>
6
		<version>1.0.1-SNAPSHOT</version>
7
	</parent>
8
	<artifactId>org.gvsig.lidar.prov.common</artifactId>
9
	<name>org.gvsig.lidar.prov.common</name>
10
	<dependencies>
11
		<dependency>
12
			<groupId>org.gvsig</groupId>
13
			<artifactId>org.gvsig.fmap.dal.file.lib</artifactId>
14
		</dependency>
15
		<dependency>
16
			<groupId>org.gvsig</groupId>
17
			<artifactId>org.gvsig.fmap.dal.spi</artifactId>
18
			<scope>compile</scope>
19
		</dependency>
20
		<dependency>
21
			<groupId>org.gvsig</groupId>
22
			<artifactId>org.gvsig.tools.lib</artifactId>
23
			<scope>compile</scope>
24
		</dependency>
25
                <dependency>
26
                        <groupId>commons-io</groupId>
27
                        <artifactId>commons-io</artifactId>
28
                </dependency>
29
	</dependencies>
30
</project>
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.jgrass/src/main/resources/org/gvsig/lidar/prov/jgrasstools/JGrassLASMetadata.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="JGrassLASDataStoreProvider" namespace="Metadata">
10
      <extends>
11
      	<class namespace="Metadata" name="SpatialProvider"/>
12
      </extends>
13
      <description>Metadata of a LIDAR LAS provider</description>
14
      <fields>
15
      </fields>
16
    </class>
17

  
18
  </classes>
19
</definitions>  
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.jgrass/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.lidar.prov.jgrasstools.JGrassLASLibrary
org.gvsig.lidar/trunk/org.gvsig.lidar/org.gvsig.lidar.prov/org.gvsig.lidar.prov.jgrass/src/main/java/org/gvsig/lidar/prov/jgrasstools/JGrassLASDataStoreProvider.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.lidar.prov.jgrasstools;
25

  
26
import java.io.File;
27
import java.io.IOException;
28
import java.lang.reflect.Constructor;
29
import java.lang.reflect.InvocationTargetException;
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.Date;
33
import java.util.Iterator;
34
import java.util.List;
35
import java.util.Set;
36

  
37
import org.apache.commons.io.FileUtils;
38
import org.cresques.cts.ICRSFactory;
39
import org.cresques.cts.IProjection;
40
import org.gvsig.fmap.dal.DALLocator;
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.feature.EditableFeatureAttributeDescriptor;
49
import org.gvsig.fmap.dal.feature.EditableFeatureType;
50
import org.gvsig.fmap.dal.feature.Feature;
51
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
52
import org.gvsig.fmap.dal.feature.FeatureSet;
53
import org.gvsig.fmap.dal.feature.FeatureType;
54
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
55
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
56
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
57
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
58
import org.gvsig.fmap.dal.resource.ResourceAction;
59
import org.gvsig.fmap.dal.resource.exception.ResourceException;
60
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
61
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
62
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
63
import org.gvsig.fmap.geom.Geometry.TYPES;
64
import org.gvsig.fmap.geom.GeometryLocator;
65
import org.gvsig.fmap.geom.GeometryManager;
66
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
67
import org.gvsig.fmap.geom.exception.CreateGeometryException;
68
import org.gvsig.fmap.geom.primitive.Envelope;
69
import org.gvsig.fmap.geom.primitive.Point;
70
import org.gvsig.fmap.geom.type.GeometryType;
71
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
72
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
73
import org.gvsig.lidar.prov.LASDataStoreParameters;
74
import org.gvsig.lidar.prov.LASDataStoreProvider;
75
import org.gvsig.lidar.prov.ConversionUtils;
76
import org.gvsig.lidar.prov.LASUnsupportedFormatException;
77
import org.gvsig.tools.dispose.DisposableIterator;
78
import org.gvsig.tools.dispose.DisposeUtils;
79
import org.gvsig.tools.dynobject.DynObject;
80
import org.gvsig.tools.exception.BaseException;
81
import org.jgrasstools.gears.io.las.core.ALasReader;
82
import org.jgrasstools.gears.io.las.core.ILasHeader;
83
import org.jgrasstools.gears.io.las.core.LasRecord;
84
import org.jgrasstools.gears.io.las.core.liblas.LiblasReader;
85
import org.jgrasstools.gears.io.las.core.v_1_0.LasReader;
86
import org.jgrasstools.gears.io.las.core.v_1_0.LasWriter;
87
import org.opengis.metadata.Identifier;
88
import org.opengis.metadata.extent.Extent;
89
import org.opengis.referencing.crs.CRSFactory;
90
import org.opengis.referencing.crs.CoordinateReferenceSystem;
91
import org.opengis.referencing.cs.CoordinateSystem;
92
import org.opengis.util.GenericName;
93
import org.opengis.util.InternationalString;
94
import org.slf4j.Logger;
95
import org.slf4j.LoggerFactory;
96

  
97
import com.sun.jna.Native;
98
import com.sun.jna.NativeLibrary;
99

  
100
/**
101
 * 
102
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
103
 *
104
 */
105
public class JGrassLASDataStoreProvider extends LASDataStoreProvider {
106

  
107
    private static final Logger LOG = LoggerFactory.getLogger(JGrassLASDataStoreProvider.class);
108

  
109
    public static final String NAME = "JGrassLASDataStoreProvider";
110

  
111
    public static final String DESCRIPTION = "LIDAR LAS file (jgrasstools)";
112
    
113
    public static final String METADATA_DEFINITION_NAME = "JGrassLASDataStoreProvider";
114

  
115
    protected ALasReader reader = null;
116

  
117
    protected JGrassLASDataStoreProvider(DataStoreParameters dataParameters,
118
        DataStoreProviderServices storeServices, DynObject metadata) throws InitializeException {
119
        super(dataParameters, storeServices, metadata);
120
    }
121

  
122
    protected JGrassLASDataStoreProvider(DataStoreParameters dataParameters,
123
        DataStoreProviderServices storeServices) throws InitializeException {
124
    	
125
    	super(dataParameters, storeServices, FileHelper
126
    	        .newMetadataContainer(METADATA_DEFINITION_NAME));
127
    }
128
    
129
    @Override
130
    public String getFullName() {
131
    	return getName();
132
    }
133

  
134
    @Override
135
    public String getName() {
136
        return getLASParameters().getFile().getName();
137
    }
138

  
139
    @Override
140
    public String getProviderName() {
141
        return NAME;
142
    }
143

  
144
    @Override
145
    public boolean allowWrite() {
146
    	return true;
147
    }
148

  
149
    protected boolean loadFeatureType() throws LASUnsupportedFormatException,
150
        GeometryTypeNotSupportedException, GeometryTypeNotValidException, DataException {
151

  
152
        return (boolean) getResource().execute(new ResourceAction() {
153

  
154
            @Override
155
            public Object run() throws Exception {
156
            	EditableFeatureType editableFeatureType = DALLocator.getDataManager().createFeatureType();
157
                editableFeatureType.setHasOID(true);
158
                EditableFeatureAttributeDescriptor attributeDescriptor = editableFeatureType.add("GEOM",  DataTypes.GEOMETRY);
159
                
160
                GeometryManager geometryManager = GeometryLocator.getGeometryManager();
161
                GeometryType geometryType =
162
                        geometryManager.getGeometryType(
163
                        		TYPES.POINT, SUBTYPES.GEOM3D);
164
                attributeDescriptor.setGeometryType(geometryType);
165
                if (getLASParameters().getCRS()!=null) {
166
                	attributeDescriptor.setSRS(getLASParameters().getCRS());
167
                }
168
                editableFeatureType.setDefaultGeometryAttributeName(JGrassLASDataStoreProvider.GEOM_FIELD);
169
                attributeDescriptor =
170
                        editableFeatureType.add(CLASS_FIELD, DataTypes.BYTE);
171
                attributeDescriptor =
172
                        editableFeatureType.add(POINTX_FIELD, DataTypes.DOUBLE);
173
                attributeDescriptor =
174
                        editableFeatureType.add(POINTY_FIELD, DataTypes.DOUBLE);
175
                attributeDescriptor =
176
                        editableFeatureType.add(POINTZ_FIELD, DataTypes.DOUBLE);
177
                attributeDescriptor =
178
                        editableFeatureType.add(INTENSITY_FIELD, DataTypes.INT);
179
                attributeDescriptor =
180
                        editableFeatureType.add(RETURNNUM_FIELD, DataTypes.BYTE);
181
                attributeDescriptor =
182
                        editableFeatureType.add(NUMRETURNS_FIELD, DataTypes.BYTE);
183
                
184
                ILasHeader header = getReader().getHeader();
185
                if (header.hasRGB()) {
186
                	attributeDescriptor =
187
                            editableFeatureType.add(COLOR_FIELD, DataTypes.INT);
188
                }
189
                if (header.hasGpsTime()) {
190
                	if (header.getGpsTimeType()==1) {
191
                		// Show adjusted standard GPS time as a Java Date
192
                		attributeDescriptor = editableFeatureType.add(TIME_FIELD, DataTypes.DATE);
193
                	}
194
                	else {
195
                		// Show GPS week time as a plain long, as we don't have enough context to translate it to a real date
196
                		attributeDescriptor = editableFeatureType.add(WEEKTIME_FIELD, DataTypes.LONG);
197
                	}
198
                }
199

  
200
                FeatureType featureType = editableFeatureType.getNotEditableCopy();
201
                if (featureType.getDefaultSRS() != null) {
202
                    setDynValue(DataStore.METADATA_CRS, featureType.getDefaultSRS());
203
                }
204

  
205
                List<FeatureType> featureTypes = new ArrayList<FeatureType>(1);
206
                featureTypes.add(featureType);
207

  
208
                getStoreServices().setFeatureTypes(featureTypes, featureType);
209
                
210
                return true;
211
            }
212
        });
213
    }
214

  
215
    public FeatureProvider internalGetFeatureProviderByIndex(
216
    		long index, FeatureType featureType)
217
    				throws DataException {
218
    	
219
    	long decimatedIndex = (long) Math.floor(index * decimation);
220
    	FeatureProvider featureProvider = this.createFeatureProvider(featureType);
221
    	featureProvider.setOID(new Long(decimatedIndex));
222
    	LasRecord record;
223
		try {
224
			record = getReader().getPointAt(decimatedIndex);
225
		} catch (IOException e1) {
226
			throw new FeatureIndexException("Error accessing record", e1,
227
					"Error accessing record", -1);
228
		}
229
		if (featureType.getAttributeDescriptor(GEOM_FIELD)!=null) {
230
			try {
231
				Point p = (Point) this.gm.create(TYPES.POINT, SUBTYPES.GEOM3D);
232
				p.setCoordinateAt(0, record.x);
233
				p.setCoordinateAt(1, record.y);
234
				p.setCoordinateAt(2, record.z);
235
				featureProvider.set(GEOM_FIELD, p);
236
			} catch (CreateGeometryException e) {
237
				// TODO Auto-generated catch block
238
				e.printStackTrace();
239
			}
240
		}
241
    	if (featureType.getAttributeDescriptor(POINTX_FIELD)!=null) {
242
    		featureProvider.set(POINTX_FIELD, record.x);
243
    	}
244
    	if (featureType.getAttributeDescriptor(POINTY_FIELD)!=null) {
245
    		featureProvider.set(POINTY_FIELD, record.y);
246
    	}
247
    	if (featureType.getAttributeDescriptor(POINTZ_FIELD)!=null) {
248
    		featureProvider.set(POINTZ_FIELD, record.z);
249
    	}
250
    	if (featureType.getAttributeDescriptor(CLASS_FIELD)!=null) {
251
    		featureProvider.set(CLASS_FIELD, record.classification);
252
    	}
253
    	if (featureType.getAttributeDescriptor(INTENSITY_FIELD)!=null) {
254
    		featureProvider.set(INTENSITY_FIELD, (int)record.intensity);
255
    	}
256
    	if (featureType.getAttributeDescriptor(RETURNNUM_FIELD)!=null) {
257
    		featureProvider.set(RETURNNUM_FIELD, (byte)record.returnNumber);
258
    	}
259
    	if (featureType.getAttributeDescriptor(NUMRETURNS_FIELD)!=null) {
260
    		featureProvider.set(NUMRETURNS_FIELD, (byte) record.numberOfReturns);
261
    	}
262
    	if (featureType.getAttributeDescriptor(TIME_FIELD)!=null) {
263
    		long las_time  = (long)record.gpsTime;
264
    		// file uses LAS 1.2/1.3/1.4 adjusted GPS Time
265
    		// convert to unix time and build a Date object
266
    		Date unix_time = new Date(las_time + ConversionUtils.LAS_TIME_TO_UNIX_TIME);
267
			featureProvider.set(TIME_FIELD, unix_time);
268
    	}
269
    	if (featureType.getAttributeDescriptor(WEEKTIME_FIELD)!=null) {
270
    		// LAS 1.0, 1.1 gps week time, we handle it as a plain long.
271
    		// It could be translated to Unix time using the following formulas:
272
    		// seconds_per_week = 604800
273
    		// las_gps_time = las_week_time + ( week_number_from_6_1_1980 * seconds_per_week)
274
    		// unix_time = las_gps_time + 315964800
275
    		long las_time  = (long)record.gpsTime;
276
    		featureProvider.set(WEEKTIME_FIELD, new Long(las_time));
277
    	}
278
    	if (featureType.getAttributeDescriptor(COLOR_FIELD)!=null) {
279

  
280
    		int red = record.color[0];
281
    		int green = record.color[1];
282
    		int blue = record.color[2];
283
    		// RGB value representing the color in the default sRGB ColorModel.
284
    		// (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue).
285
    		featureProvider.set(COLOR_FIELD, ConversionUtils.rgbToInt(red, green, blue));
286
    	}
287
    	return featureProvider;
288
    }
289
    
290
    protected synchronized ALasReader getReader() throws LASUnsupportedFormatException, OpenException {
291
        if (this.reader == null) {
292
        	/**
293
        	 * We create a fake geoapi CRS, because providing a null CRS to JGrasstools readers
294
        	 * causes errors if the .prj file exists and the dependences are not satisfied
295
        	 */
296
        	CoordinateReferenceSystemFakeImpl crs = new CoordinateReferenceSystemFakeImpl();
297
        	try {
298
        		if (ALasReader.supportsNative()) {
299
        			reader = new LiblasReader(file, crs);
300
        			LOG.info("JGrass LiblasReader loaded");
301
        		}
302
        	} catch (Exception exc) {
303
        		LOG.debug("Error loading native liblas reader", exc);
304
        	}
305
        	if (reader==null) {
306
        		try {
307
        			reader = new LasReader(file, crs);
308
        			LOG.info("JGrass LasReader loaded");
309
        		}
310
        		catch (Exception e) {
311
        			throw new OpenException("Error loading Java JGrass LAS reader", e);
312
        		}
313
        	}
314
        	try {
315
        		reader.open();
316
        	} catch (Exception e) {
317
        		throw new OpenException("Error opening JGrass LAS reader", e);
318
        	}
319
        }
320
        return this.reader;
321
    }
322
    
323
    /*
324
     * Lazy envelope initialization
325
     */
326
    @Override
327
    public Envelope getEnvelope() throws DataException {
328
    	
329
        open();
330
        if (this.envelope == null) {
331
        	Envelope env;
332
			try {
333
				env = this.gm.createEnvelope(SUBTYPES.GEOM3D);
334
	        	Point p1 = (Point) this.gm.create(TYPES.POINT, SUBTYPES.GEOM3D);
335
	        	Point p2 = (Point) this.gm.create(TYPES.POINT, SUBTYPES.GEOM3D);
336
	        	// FIXME: do we need to consider projection for upper & lower corner?
337
	        	ILasHeader header = getReader().getHeader();
338
	        	double[] env3d = header.getRawDataEnvelope();
339
	        	p1.setCoordinateAt(0, env3d[0]);
340
	        	p1.setCoordinateAt(1, env3d[1]);
341
	        	p1.setCoordinateAt(2, env3d[2]);
342
	        	p2.setCoordinateAt(0, env3d[3]);
343
	        	p2.setCoordinateAt(1, env3d[4]);
344
	        	p2.setCoordinateAt(2, env3d[5]);
345
	        	env.setLowerCorner(p1);
346
	        	env.setUpperCorner(p2);
347
	        	this.envelope = env;
348
			} catch (CreateEnvelopeException e) {
349
				// TODO Auto-generated catch block
350
				e.printStackTrace();
351
			} catch (CreateGeometryException e) {
352
				// TODO Auto-generated catch block
353
				e.printStackTrace();
354
			}
355

  
356
        }
357
        return this.envelope;
358
    }
359

  
360

  
361
    @Override
362
    public long getFeatureCount() throws DataException {
363
        open();
364
        return ((Number) getResource().execute(new ResourceAction() {
365

  
366
            @Override
367
            public Object run() throws Exception {
368
            	long count = getReader().getHeader().getRecordsCount();
369
            	if (decimation>1.0d) {
370
            		count = (long) Math.floor((count/decimation));
371
            	}
372
            	return count;
373
            }
374
        })).longValue();
375
    }
376
    
377
    @Override
378
    protected void doDispose() throws BaseException {
379
        super.doDispose();
380
        getResource().removeConsumer(this);
381
        this.resourceProvider = null;
382
        this.gm = null;
383
        this.envelope = null;
384
        this.opened = false;
385
        try { this.reader.close(); } catch (Exception e) {}
386
        this.reader = null;
387
    }
388

  
389
    @Override
390
    public boolean closeResourceRequested(ResourceProvider resource) {
391
        this.envelope = null;
392
        this.opened = false;
393
        try { this.reader.close(); } catch (Exception e) {}
394
        this.reader = null;
395
        return true;
396
    }
397

  
398
    @Override
399
    public void resourceChanged(ResourceProvider resource) {
400
        this.envelope = null;
401
        this.opened = false;
402
        try { this.reader.close(); } catch (Exception e) {}
403
        this.reader = null;
404
    }
405

  
406
    @SuppressWarnings("rawtypes")
407
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds,
408
        Iterator originalFeatureTypesUpdated) throws PerformEditingException {
409

  
410
        final FeatureType fType;
411
        try {
412
            fType = this.getStoreServices().getDefaultFeatureType();
413
        } catch (DataException e) {
414
            throw new PerformEditingException(this.getProviderName(), e);
415
        }
416
        final FeatureAttributeDescriptor geomAttrib = fType.getDefaultGeometryAttribute();
417
        
418
        if (geomAttrib.getGeomType().getType()!=TYPES.POINT) {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff