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.csv / src / main / java / org / gvsig / fmap / dal / store / gml / simplereaders / GMLReader.java @ 47652
History | View | Annotate | Download (7.17 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 org.gvsig.fmap.dal.store.simplereader.simplereaders.AbstractSimpleReader; |
23 |
import org.gvsig.tools.ToolsLocator; |
24 |
import org.gvsig.tools.dataTypes.DataType; |
25 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
26 |
import org.gvsig.tools.namestranslator.NamesTranslator; |
27 |
import org.gvsig.tools.task.SimpleTaskStatus; |
28 |
import org.gvsig.tools.util.GetItemWithSize64; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
/**
|
33 |
*
|
34 |
* @author gvSIG Team
|
35 |
*/
|
36 |
public class GMLReader extends AbstractSimpleReader { |
37 |
|
38 |
private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class); |
39 |
|
40 |
private XMLFileAsList gml;
|
41 |
private int columns; |
42 |
private String[] header; |
43 |
private int currentElement = 0; |
44 |
private final GMLStoreParameters parameters; |
45 |
private GfsFile gfs;
|
46 |
|
47 |
public GMLReader(Reader reader, GMLStoreParameters theParameters) throws IOException { |
48 |
File gmlFile = theParameters.getFile();
|
49 |
File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gmlidx"); |
50 |
File gfsFile = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gfs"); |
51 |
gfs = new GfsFile();
|
52 |
if(gfsFile.exists()){
|
53 |
//TODO: Comparar fechas del gfs y gml
|
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 |
@Override
|
74 |
public String[] getHeader() throws IOException { |
75 |
if (this.header == null) { |
76 |
List<String> geoms = gfs.getGeometryElementPaths(); |
77 |
String[] theHeader = new String[gfs.size()+(geoms==null?0:geoms.size())]; |
78 |
int i = 0; |
79 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
80 |
for (GfsFile.PropertyDefn item : gfs) {
|
81 |
DataType dataType = dataTypesManager.get(item.getType()); |
82 |
theHeader[i++] = item.getName()+"/"+dataType.getName();
|
83 |
} |
84 |
if(geoms != null) { |
85 |
List<String> srss = gfs.getGeometryElementSrss(); |
86 |
if(this.parameters.getGeometryCombineMode() == XMLFileAsList.COMBINE_FIRST_NOT_NULL){ |
87 |
String srs = srss.get(0); |
88 |
for (int j = 0; j < srss.size(); j++) { |
89 |
if(!StringUtils.equalsIgnoreCase(srs, srss.get(j))){
|
90 |
srs = null;
|
91 |
break;
|
92 |
} |
93 |
} |
94 |
IProjection proj = null;
|
95 |
if(srs != null){ |
96 |
proj = CRSFactory.getCRS(srs); |
97 |
} |
98 |
if(proj == null){ |
99 |
theHeader[i++] = FilenameUtils.getBaseName(getGeometryAttributeName())+"/Geometry";
|
100 |
} else {
|
101 |
theHeader[i++] = FilenameUtils.getBaseName(getGeometryAttributeName())+"/Geometry/set/srs="+proj.getAbrev().replace(":", "@"); |
102 |
} |
103 |
} else {
|
104 |
for (int j = 0; j < srss.size(); j++) { |
105 |
IProjection srs = CRSFactory.getCRS(srss.get(j)); |
106 |
if(srs == null){ |
107 |
theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j))+"/Geometry";
|
108 |
} else {
|
109 |
theHeader[i++] = FilenameUtils.getBaseName(geoms.get(j))+"/Geometry/set/srs="+srs.getAbrev().replace(":", "@"); |
110 |
} |
111 |
} |
112 |
} |
113 |
} |
114 |
this.header = theHeader;
|
115 |
} |
116 |
return this.header; |
117 |
} |
118 |
|
119 |
private String getGeometryAttributeName() { |
120 |
NamesTranslator translator = NamesTranslator.createBaseTranslator(); |
121 |
for (GfsFile.PropertyDefn item : gfs) {
|
122 |
translator.addSource(item.getName()); |
123 |
} |
124 |
String s = translator.getSuggestion("Geometry"); |
125 |
return s;
|
126 |
} |
127 |
|
128 |
@Override
|
129 |
public int getColumnsCount() throws IOException { |
130 |
if (this.columns <= 0) { |
131 |
this.columns = this.getHeader().length; |
132 |
} |
133 |
return this.columns; |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public List<String> read() throws IOException { |
138 |
List<String> values = this.read(currentElement); |
139 |
if (values == null) { |
140 |
return null; |
141 |
} |
142 |
currentElement += 1;
|
143 |
return values;
|
144 |
} |
145 |
|
146 |
public List<String> read(int rowNumber) throws IOException { |
147 |
if (rowNumber < this.getGml().size64()) { |
148 |
List<String> values = this.getGml().get64(rowNumber); |
149 |
return values;
|
150 |
} |
151 |
return null; |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void close() throws IOException { |
156 |
IOUtils.closeQuietly(this.gml);
|
157 |
this.gml = null; |
158 |
this.gfs = null; |
159 |
} |
160 |
|
161 |
@Override
|
162 |
public List<String> skip(int lines) throws IOException { |
163 |
this.currentElement += lines;
|
164 |
if (this.currentElement > this.getGml().size64()) { |
165 |
return null; |
166 |
} |
167 |
return read(this.currentElement); |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public int getLine() { |
172 |
if (this.getGml() == null) { |
173 |
return 0; |
174 |
} |
175 |
return this.currentElement; |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public List<String> nextRowValues() { |
180 |
try {
|
181 |
return this.read(); |
182 |
} catch (IOException ex) { |
183 |
throw new RuntimeException(ex); |
184 |
} |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public GetItemWithSize64<List<String>> getVirtualRows(SimpleTaskStatus status) { |
189 |
try {
|
190 |
File gmlFile = this.parameters.getFile(); |
191 |
File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath()) + ".gmlidx"); |
192 |
|
193 |
XMLFileAsList x = new XMLFileAsList(
|
194 |
gmlFile, |
195 |
gmlIdx, |
196 |
Charset.forName(SimpleReaderStoreParameters.getCharset(this.parameters)), |
197 |
gfs.getBaseElementPath(), |
198 |
gfs.getGeometryElementPaths(), |
199 |
gfs.getPropertiesPaths() |
200 |
); |
201 |
|
202 |
x.setCombineMode(this.parameters.getGeometryCombineMode());
|
203 |
|
204 |
return x;
|
205 |
} catch (IOException ex) { |
206 |
throw new RuntimeException("Can't get virtual rows", ex); |
207 |
} |
208 |
} |
209 |
} |