gvsig-projects-pool / org.gvsig.lidar.prov / org.gvsig.lidar.prov.jgrass / src / main / java / org / gvsig / lidar / prov / jgrasstools / JGrassLASFilesystemServerProvider.java @ 281
History | View | Annotate | Download (9.18 KB)
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 |
|
28 |
import org.cresques.cts.IProjection; |
29 |
import org.gvsig.fmap.dal.DALLocator; |
30 |
import org.gvsig.fmap.dal.DataServerExplorer; |
31 |
import org.gvsig.fmap.dal.DataStoreParameters; |
32 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
33 |
import org.gvsig.fmap.dal.exception.CreateException; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.exception.InitializeException; |
36 |
import org.gvsig.fmap.dal.exception.RemoveException; |
37 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
38 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
39 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
40 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
41 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
42 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
43 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
44 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
45 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
46 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
47 |
import org.gvsig.lidar.prov.LASDataStoreParameters; |
48 |
import org.gvsig.lidar.prov.LASDataStoreProvider; |
49 |
import org.jgrasstools.gears.io.las.core.v_1_0.LasWriter; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
/**
|
54 |
*
|
55 |
* @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
|
56 |
*
|
57 |
*/
|
58 |
public class JGrassLASFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider implements |
59 |
FilesystemServerExplorerProvider, ResourceConsumer { |
60 |
|
61 |
private static final Logger LOG = LoggerFactory.getLogger(JGrassLASFilesystemServerProvider.class); |
62 |
|
63 |
private FilesystemServerExplorerProviderServices serverExplorer;
|
64 |
|
65 |
@Override
|
66 |
public String getDataStoreProviderName() { |
67 |
return JGrassLASDataStoreProvider.NAME;
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
public String getDescription() { |
72 |
return JGrassLASDataStoreProvider.DESCRIPTION;
|
73 |
} |
74 |
|
75 |
@Override
|
76 |
public boolean accept(File pathname) { |
77 |
return (pathname.getName().toLowerCase().endsWith(".las")); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public boolean closeResourceRequested(ResourceProvider resource) { |
82 |
// Do nothing
|
83 |
return true; |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public void resourceChanged(ResourceProvider resource) { |
88 |
// Do nothing
|
89 |
} |
90 |
|
91 |
@Override
|
92 |
public void initialize(FilesystemServerExplorerProviderServices serverExplorer) { |
93 |
this.serverExplorer = serverExplorer;
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public boolean canCreate() { |
98 |
return true; |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void create(NewDataStoreParameters parameters, boolean overwrite) throws CreateException { |
103 |
if (parameters instanceof LASDataStoreParameters) { |
104 |
|
105 |
final LASDataStoreParameters newLASDataStoreParameters =
|
106 |
(LASDataStoreParameters) parameters; |
107 |
|
108 |
final File file = newLASDataStoreParameters.getFile(); |
109 |
|
110 |
if (file.isFile() && file.exists()) {
|
111 |
if (overwrite) {
|
112 |
// Deleting existing file
|
113 |
try {
|
114 |
this.remove(newLASDataStoreParameters);
|
115 |
} catch (RemoveException e) {
|
116 |
throw new CreateException("Can not overwrite file", e); |
117 |
} |
118 |
} else {
|
119 |
throw new CreateException(file.getAbsolutePath(), new IllegalArgumentException( |
120 |
String.format("%1s exists and overwrite option is false", |
121 |
file.getAbsolutePath()))); |
122 |
} |
123 |
} |
124 |
|
125 |
try {
|
126 |
final FileResource resource =
|
127 |
(FileResource) serverExplorer.getServerExplorerProviderServices() |
128 |
.createResource(FileResource.NAME, new Object[] { file.getAbsolutePath() }); |
129 |
|
130 |
resource.addConsumer(this);
|
131 |
resource.execute(new ResourceAction() {
|
132 |
|
133 |
@Override
|
134 |
public Object run() throws Exception { |
135 |
|
136 |
FeatureAttributeDescriptor geometryAttribute = |
137 |
newLASDataStoreParameters.getDefaultFeatureType() |
138 |
.getDefaultGeometryAttribute(); |
139 |
|
140 |
if (geometryAttribute != null) { |
141 |
if (TYPES.POINT!=geometryAttribute.getGeomType().getType()) {
|
142 |
throw new IllegalArgumentException( |
143 |
"LAS files can only store points.");
|
144 |
} |
145 |
|
146 |
IProjection projection = geometryAttribute.getSRS(); |
147 |
if (projection == null && newLASDataStoreParameters.getCRS() != null) { |
148 |
projection = newLASDataStoreParameters.getCRS(); |
149 |
} |
150 |
} |
151 |
EditableFeatureType featureType = newLASDataStoreParameters.getDefaultFeatureType(); |
152 |
JGrassLASWriter writer = new JGrassLASWriter(newLASDataStoreParameters, featureType, null); |
153 |
writer.open(); |
154 |
writer.close(); |
155 |
return null; |
156 |
} |
157 |
}); |
158 |
} catch (InitializeException e) {
|
159 |
throw new CreateException("Can not create file resource to create it", e); |
160 |
} |
161 |
} |
162 |
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
167 |
LASDataStoreParameters newParameters = new LASDataStoreParameters(JGrassLASDataStoreProvider.NAME);
|
168 |
EditableFeatureType editableFeatureType = DALLocator.getDataManager().createFeatureType(); |
169 |
newParameters.setDefaultFeatureType(editableFeatureType); |
170 |
return newParameters;
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void remove(DataStoreParameters parameters) throws RemoveException { |
175 |
|
176 |
if (parameters instanceof LASDataStoreParameters) { |
177 |
|
178 |
LASDataStoreParameters newLASDataStoreParameters = |
179 |
(LASDataStoreParameters) parameters; |
180 |
|
181 |
final File file = newLASDataStoreParameters.getFile(); |
182 |
|
183 |
if (!file.exists() && !file.canWrite()) {
|
184 |
throw new RemoveException(file.getAbsolutePath(), new IllegalArgumentException( |
185 |
String.format("%1s does not exist", file.getAbsolutePath()))); |
186 |
} |
187 |
|
188 |
try {
|
189 |
final FileResource resource =
|
190 |
(FileResource) serverExplorer.getServerExplorerProviderServices() |
191 |
.createResource(FileResource.NAME, new Object[] { file.getAbsolutePath() }); |
192 |
|
193 |
resource.addConsumer(this);
|
194 |
resource.execute(new ResourceAction() {
|
195 |
|
196 |
@Override
|
197 |
public Object run() throws Exception { |
198 |
resource.closeRequest(); |
199 |
file.delete(); |
200 |
return null; |
201 |
} |
202 |
}); |
203 |
resource.addConsumer(this);
|
204 |
} catch (InitializeException e) {
|
205 |
throw new RemoveException("Can not create file resource to remove it", e); |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public boolean canCreate(NewDataStoreParameters parameters) { |
212 |
|
213 |
if (parameters instanceof LASDataStoreParameters) { |
214 |
|
215 |
LASDataStoreParameters newLASDataStoreParameters = |
216 |
(LASDataStoreParameters) parameters; |
217 |
|
218 |
File file = newLASDataStoreParameters.getFile();
|
219 |
if (file != null && file.canWrite()) { |
220 |
return true; |
221 |
} |
222 |
|
223 |
FeatureAttributeDescriptor geometryAttribute = |
224 |
newLASDataStoreParameters.getDefaultFeatureType() |
225 |
.getDefaultGeometryAttribute(); |
226 |
|
227 |
if (geometryAttribute != null) { |
228 |
if (TYPES.POINT!=geometryAttribute.getGeomType().getType()) {
|
229 |
throw new IllegalArgumentException( |
230 |
"LAS files can only store points.");
|
231 |
} |
232 |
|
233 |
} |
234 |
} |
235 |
return false; |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public int getMode() { |
240 |
return DataServerExplorer.MODE_GEOMETRY|DataServerExplorer.MODE_FEATURE;
|
241 |
} |
242 |
} |