svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.gml / src / main / java / org / gvsig / fmap / dal / store / gml / simplereaders / GMLReader.java @ 47665
History | View | Annotate | Download (7.92 KB)
1 |
/*
|
---|---|
2 |
* To change this license theHeader, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.dal.store.gml.simplereaders; |
7 |
|
8 |
import java.io.File; |
9 |
import java.io.IOException; |
10 |
import java.io.Reader; |
11 |
import java.nio.charset.Charset; |
12 |
import java.util.List; |
13 |
import org.apache.commons.io.FilenameUtils; |
14 |
import org.apache.commons.io.IOUtils; |
15 |
import org.apache.commons.lang3.StringUtils; |
16 |
import org.cresques.cts.IProjection; |
17 |
import org.gvsig.fmap.crs.CRSFactory; |
18 |
import org.gvsig.fmap.dal.store.gml.GMLStoreParameters; |
19 |
import org.gvsig.fmap.dal.store.gml.virtualrows.GfsFile; |
20 |
import org.gvsig.fmap.dal.store.gml.virtualrows.XMLFileAsList; |
21 |
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters; |
22 |
import static org.gvsig.fmap.dal.store.simplereader.SimpleReaderUtils.isFileNewer; |
23 |
import org.gvsig.fmap.dal.store.simplereader.simplereaders.AbstractSimpleReader; |
24 |
import org.gvsig.tools.ToolsLocator; |
25 |
import org.gvsig.tools.dataTypes.DataType; |
26 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
27 |
import org.gvsig.tools.namestranslator.NamesTranslator; |
28 |
import org.gvsig.tools.task.SimpleTaskStatus; |
29 |
import org.gvsig.tools.util.GetItemWithSize64; |
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
/**
|
34 |
*
|
35 |
* @author gvSIG Team
|
36 |
*/
|
37 |
public class GMLReader extends AbstractSimpleReader { |
38 |
|
39 |
private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class); |
40 |
|
41 |
private XMLFileAsList gml;
|
42 |
private int columns; |
43 |
private String[] header; |
44 |
private int currentElement = 0; |
45 |
private final GMLStoreParameters parameters; |
46 |
private GfsFile gfs;
|
47 |
|
48 |
public GMLReader(Reader reader, GMLStoreParameters theParameters) throws IOException { |
49 |
File gmlFile = theParameters.getFile();
|
50 |
File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gmlidx"); |
51 |
File gfsFile = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gfs"); |
52 |
gfs = new GfsFile();
|
53 |
if(gfsFile.exists() && isFileNewer(gfsFile, gmlFile)){
|
54 |
gfs.load(gfsFile); |
55 |
} else {
|
56 |
gfs.fetch(gmlFile); |
57 |
gfs.save(gfsFile); |
58 |
} |
59 |
|
60 |
this.gml = null; |
61 |
this.columns = -1; |
62 |
this.header = null; |
63 |
this.parameters = theParameters;
|
64 |
} |
65 |
|
66 |
private XMLFileAsList getGml(){
|
67 |
if(this.gml == null){ |
68 |
this.gml = (XMLFileAsList) this.getVirtualRows(SimpleTaskStatus.FAKE_STATUS); |
69 |
} |
70 |
return this.gml; |
71 |
} |
72 |
|
73 |
private IProjection toProjection(String srs){ |
74 |
IProjection proj; |
75 |
// http://www.opengis.net/def/crs/EPSG/0/25830
|
76 |
try {
|
77 |
if(StringUtils.startsWithIgnoreCase(srs, "http://www.opengis.net/def/crs")){ |
78 |
String[] ss = StringUtils.split(srs, "/"); |
79 |
String crs = ss[ss.length-3]+":"+ss[ss.length-1]; |
80 |
proj = CRSFactory.getCRS(crs); |
81 |
} else {
|
82 |
proj = CRSFactory.getCRS(srs); |
83 |
} |
84 |
} catch (Throwable th) { |
85 |
return null; |
86 |
} |
87 |
|
88 |
return proj;
|
89 |
|
90 |
} |
91 |
|
92 |
@Override
|
93 |
public String[] getHeader() throws IOException { |
94 |
if (this.header == null) { |
95 |
List<String> geoms = gfs.getGeometryElementPaths(); |
96 |
String[] theHeader = new String[gfs.size()+(geoms==null?0:geoms.size())]; |
97 |
int i = 0; |
98 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
99 |
for (GfsFile.PropertyDefn item : gfs) {
|
100 |
DataType dataType = dataTypesManager.get(item.getType()); |
101 |
theHeader[i++] = item.getName()+"/"+dataType.getName();
|
102 |
} |
103 |
if(geoms != null) { |
104 |
List<String> srss = gfs.getGeometryElementSrss(); |
105 |
if(this.parameters.getGeometryCombineMode() == XMLFileAsList.COMBINE_FIRST_NOT_NULL){ |
106 |
String srs = srss.get(0); |
107 |
for (int j = 0; j < srss.size(); j++) { |
108 |
if(!StringUtils.equalsIgnoreCase(srs, srss.get(j))){
|
109 |
srs = null;
|
110 |
break;
|
111 |
} |
112 |
} |
113 |
IProjection proj = null;
|
114 |
if(srs != null){ |
115 |
proj = toProjection(srs); |
116 |
} |
117 |
if(proj == null){ |
118 |
theHeader[i++] = FilenameUtils.getBaseName(getGeometryAttributeName())+"/Geometry";
|
119 |
} else {
|
120 |
theHeader[i++] = FilenameUtils.getBaseName(getGeometryAttributeName())+"/Geometry/set/srs="+proj.getAbrev().replace(":", "@"); |
121 |
} |
122 |
} else {
|
123 |
for (int j = 0; j < srss.size(); j++) { |
124 |
IProjection srs = toProjection(srss.get(j)); |
125 |
if(srs == null){ |
126 |
theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j))+"/Geometry";
|
127 |
} else {
|
128 |
theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j))+"/Geometry/set/srs="+srs.getAbrev().replace(":", "@"); |
129 |
} |
130 |
} |
131 |
} |
132 |
} |
133 |
this.header = theHeader;
|
134 |
} |
135 |
return this.header; |
136 |
} |
137 |
|
138 |
private String getGeometryAttributeName() { |
139 |
NamesTranslator translator = NamesTranslator.createBaseTranslator(); |
140 |
for (GfsFile.PropertyDefn item : gfs) {
|
141 |
translator.addSource(item.getName()); |
142 |
} |
143 |
String s = translator.getSuggestion("Geometry"); |
144 |
return s;
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public int getColumnsCount() throws IOException { |
149 |
if (this.columns <= 0) { |
150 |
this.columns = this.getHeader().length; |
151 |
} |
152 |
return this.columns; |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public List<String> read() throws IOException { |
157 |
List<String> values = this.read(currentElement); |
158 |
if (values == null) { |
159 |
return null; |
160 |
} |
161 |
currentElement += 1;
|
162 |
return values;
|
163 |
} |
164 |
|
165 |
public List<String> read(int rowNumber) throws IOException { |
166 |
if (rowNumber < this.getGml().size64()) { |
167 |
List<String> values = this.getGml().get64(rowNumber); |
168 |
return values;
|
169 |
} |
170 |
return null; |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void close() throws IOException { |
175 |
IOUtils.closeQuietly(this.gml);
|
176 |
this.gml = null; |
177 |
this.gfs = null; |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public List<String> skip(int lines) throws IOException { |
182 |
this.currentElement += lines;
|
183 |
if (this.currentElement > this.getGml().size64()) { |
184 |
return null; |
185 |
} |
186 |
return read(this.currentElement); |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public int getLine() { |
191 |
if (this.getGml() == null) { |
192 |
return 0; |
193 |
} |
194 |
return this.currentElement; |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public List<String> nextRowValues() { |
199 |
try {
|
200 |
return this.read(); |
201 |
} catch (IOException ex) { |
202 |
throw new RuntimeException(ex); |
203 |
} |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public GetItemWithSize64<List<String>> getVirtualRows(SimpleTaskStatus status) { |
208 |
try {
|
209 |
File gmlFile = this.parameters.getFile(); |
210 |
|
211 |
XMLFileAsList x = new XMLFileAsList(
|
212 |
gmlFile, |
213 |
Charset.forName(SimpleReaderStoreParameters.getCharset(this.parameters)), |
214 |
gfs.getBaseElementPath(), |
215 |
gfs.getGeometryElementPaths(), |
216 |
gfs.getPropertiesPaths() |
217 |
); |
218 |
|
219 |
File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath()) + ".gmlidx"); |
220 |
if(!x.loadIndex(gmlIdx, status)){
|
221 |
x.createIndex(gmlIdx, status); |
222 |
} |
223 |
|
224 |
x.setCombineMode(this.parameters.getGeometryCombineMode());
|
225 |
|
226 |
return x;
|
227 |
} catch (IOException ex) { |
228 |
throw new RuntimeException("Can't get virtual rows", ex); |
229 |
} |
230 |
} |
231 |
} |