Statistics
| Revision:

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 @ 47669

History | View | Annotate | Download (9.64 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.FileInputStream;
10
import java.io.IOException;
11
import java.io.Reader;
12
import java.nio.charset.Charset;
13
import java.util.Collection;
14
import java.util.List;
15
import org.apache.commons.io.FilenameUtils;
16
import org.apache.commons.io.IOUtils;
17
import org.apache.commons.lang3.StringUtils;
18
import org.cresques.cts.IProjection;
19
import org.gvsig.fmap.crs.CRSFactory;
20
import org.gvsig.fmap.dal.store.gml.GMLStoreParameters;
21
import org.gvsig.fmap.dal.store.gml.virtualrows.GfsFile;
22
import org.gvsig.fmap.dal.store.gml.virtualrows.XMLFileAsList;
23
import org.gvsig.fmap.dal.store.gml.virtualrows.XmlCommons;
24
import org.gvsig.fmap.dal.store.simplereader.SimpleReaderStoreParameters;
25
import static org.gvsig.fmap.dal.store.simplereader.SimpleReaderUtils.isFileNewer;
26
import org.gvsig.fmap.dal.store.simplereader.simplereaders.AbstractSimpleReader;
27
import org.gvsig.fmap.geom.GeometryUtils;
28
import org.gvsig.fmap.geom.type.GeometryType;
29
import org.gvsig.tools.ToolsLocator;
30
import org.gvsig.tools.dataTypes.DataType;
31
import org.gvsig.tools.dataTypes.DataTypesManager;
32
import org.gvsig.tools.namestranslator.NamesTranslator;
33
import org.gvsig.tools.task.SimpleTaskStatus;
34
import org.gvsig.tools.util.GetItemWithSize64;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
/**
39
 *
40
 * @author gvSIG Team
41
 */
42
public class GMLReader extends AbstractSimpleReader {
43

    
44
    private static final Logger LOGGER = LoggerFactory.getLogger(GMLReader.class);
45

    
46
    private XMLFileAsList gml;
47
    private int columns;
48
    private String[] header;
49
    private int currentElement = 0;
50
    private final GMLStoreParameters parameters;
51
    private GfsFile gfs;
52

    
53
    public GMLReader(Reader reader, GMLStoreParameters theParameters) throws IOException {
54
        File gmlFile = theParameters.getFile();
55
//        File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gmlidx");
56
        File gfsFile = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath())+".gfs");
57
        gfs = new GfsFile();
58
        if(gfsFile.exists() && isFileNewer(gfsFile, gmlFile)){
59
            gfs.load(gfsFile);
60
        } else {
61
            gfs.fetch(gmlFile);
62
            gfs.save(gfsFile);
63
        }
64
        
65
        this.gml = null;
66
        this.columns = -1;
67
        this.header = null;
68
        this.parameters = theParameters;
69
    }
70
    
71
    private XMLFileAsList getGml(){
72
        if(this.gml == null){
73
            this.gml = (XMLFileAsList) this.getVirtualRows(SimpleTaskStatus.FAKE_STATUS);
74
        }
75
        return this.gml;
76
    }
77
    
78
    private IProjection toProjection(String srs){
79
        IProjection proj;
80
//        http://www.opengis.net/def/crs/EPSG/0/25830
81
//        urn:ogc:def:crs,crs:EPSG:6.12:3068,crs:EPSG:6.12:5783
82
//        urn:ogc:def:crs:EPSG:6.12:3068
83
//        urn:ogc:def:crs:EPSG::4326
84
//        urn:ogc:def:crs:OGC:2:84 //Este formato no lo parseamos
85
        try {
86
            if(StringUtils.startsWithIgnoreCase(srs, "http://www.opengis.net/def/crs")){
87
                String[] ss = StringUtils.split(srs, "/");
88
                String crs = ss[ss.length-3]+":"+ss[ss.length-1];
89
                proj = CRSFactory.getCRS(crs);
90
            } else if(StringUtils.startsWithIgnoreCase(srs, "urn:ogc:def:crs,crs:")){
91
                String[] ss = StringUtils.split(srs, ",");
92
                String[] ss2 = StringUtils.split(ss[1],":");
93
                String crs = ss2[1]+":"+ss2[ss2.length-1];
94
                proj = CRSFactory.getCRS(crs);
95
            } else if(StringUtils.startsWithIgnoreCase(srs, "urn:ogc:def:crs:")){
96
                String[] ss = StringUtils.split(srs, ":");
97
                String crs = ss[4]+":"+ss[ss.length-1];
98
                proj = CRSFactory.getCRS(crs);
99
            } else {
100
                proj = CRSFactory.getCRS(srs);
101
            }
102
        } catch (Throwable th) {
103
            return null;
104
        }
105
        
106
        return proj;
107
        
108
    }
109

    
110
    @Override
111
    public String[] getHeader() throws IOException {
112
        if (this.header == null) {
113
            Collection<GfsFile.GeometryPropertyDefn> geoms = gfs.getGeometryElements();
114
            String[] theHeader = new String[gfs.size()+(geoms==null?0:geoms.size())];
115
            int i = 0;
116
            DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
117
            for (GfsFile.PropertyDefn item : gfs) {
118
                DataType dataType = dataTypesManager.get(item.getType());
119
                theHeader[i++] = item.getName()+"/"+dataType.getName();
120
            }
121
            if(geoms != null && !geoms.isEmpty()) {
122
                if(this.parameters.getGeometryCombineMode() == XMLFileAsList.COMBINE_FIRST_NOT_NULL){
123
                    String srs = null;
124
                    for (GfsFile.GeometryPropertyDefn geom : geoms) {
125
                        if(srs == null){
126
                            srs = geom.getSrs();
127
                        } else {
128
                            if(!StringUtils.equalsIgnoreCase(srs, geom.getSrs())){
129
                                srs = null;
130
                                break;
131
                            }
132
                        }
133
                    }
134
                    IProjection proj = null;
135
                    if(srs != null){
136
                        proj = toProjection(srs);
137
                    }
138
                    if(proj == null){
139
                        theHeader[i++] = getGeometryAttributeName()+"/Geometry";
140
                    } else {
141
                        theHeader[i++] = getGeometryAttributeName()+"/Geometry/set/srs="+proj.getAbrev().replace(":", "@");
142
                    }
143
                } else {
144
                    for (GfsFile.GeometryPropertyDefn geom : geoms) {
145
                        GeometryType geomType = geom.getGeometryType();
146
                        String geomType_s = "/set/geomtype="+GeometryUtils.getGeometryTypeName(geomType.getType())+":"+GeometryUtils.getGeometrySubtypeName(geomType.getSubType());
147
                        IProjection srs = toProjection(geom.getSrs());
148
                        if(srs == null){
149
                            theHeader[i++] = geom.getName()+"/Geometry"+geomType_s;
150
                        } else {
151
                            theHeader[i++] = geom.getName()+"/Geometry/set/srs="+srs.getAbrev().replace(":", "@")+geomType_s;
152
                        }
153
                    }
154
                }
155
            }
156
            this.header = theHeader;
157
        }
158
        return this.header;
159
    }
160
    
161
    private String getGeometryAttributeName() {
162
        NamesTranslator translator = NamesTranslator.createBaseTranslator();
163
        for (GfsFile.PropertyDefn item : gfs) {
164
            translator.addSource(item.getName());
165
        }
166
        String s = translator.getSuggestion("Geometry");
167
        return s;
168
    }
169

    
170
    @Override
171
    public int getColumnsCount() throws IOException {
172
        if (this.columns <= 0) {
173
            this.columns = this.getHeader().length;
174
        }
175
        return this.columns;
176
    }
177

    
178
    @Override
179
    public List<String> read() throws IOException {
180
        List<String> values = this.read(currentElement);
181
        if (values == null) {
182
            return null;
183
        }
184
        currentElement += 1;
185
        return values;
186
    }
187

    
188
    public List<String> read(int rowNumber) throws IOException {
189
        if (rowNumber < this.getGml().size64()) {
190
            List<String> values = this.getGml().get64(rowNumber);
191
            return values;
192
        }
193
        return null;
194
    }
195

    
196
    @Override
197
    public void close() throws IOException {
198
        IOUtils.closeQuietly(this.gml);
199
        this.gml = null;
200
        this.gfs = null;
201
    }
202

    
203
    @Override
204
    public List<String> skip(int lines) throws IOException {
205
        this.currentElement += lines;
206
        if (this.currentElement > this.getGml().size64()) {
207
            return null;
208
        }
209
        return read(this.currentElement);
210
    }
211

    
212
    @Override
213
    public int getLine() {
214
        if (this.getGml() == null) {
215
            return 0;
216
        }
217
        return this.currentElement;
218
    }
219

    
220
    @Override
221
    public List<String> nextRowValues() {
222
        try {
223
            return this.read();
224
        } catch (IOException ex) {
225
            throw new RuntimeException(ex);
226
        }
227
    }
228

    
229
    @Override
230
    public GetItemWithSize64<List<String>> getVirtualRows(SimpleTaskStatus status) {
231
        try {
232
            File gmlFile = this.parameters.getFile();
233
            String charset = SimpleReaderStoreParameters.getCharset(this.parameters);
234
            if(StringUtils.isBlank(charset)){
235
                FileInputStream fis = null;
236
                try {
237
                    fis = new FileInputStream(gmlFile);
238
                    charset = XmlCommons.detectCharsetName(fis);
239
                } catch (Throwable t) {
240
                    charset = "UTF-8";
241
                } finally {
242
                    IOUtils.closeQuietly(fis);
243
                }
244
            }
245

    
246
            XMLFileAsList x = new XMLFileAsList(
247
                gmlFile,
248
                Charset.forName(charset),
249
                gfs.getBaseElementPath(),
250
                gfs.getGeometryElementPaths(),
251
                gfs.getPropertiesPaths()
252
            );
253
            
254
            File gmlIdx = new File(FilenameUtils.removeExtension(gmlFile.getAbsolutePath()) + ".gmlidx");
255
            if(!x.loadIndex(gmlIdx, status)){
256
                x.createIndex(gmlIdx, status);
257
            }
258
            
259
            x.setCombineMode(this.parameters.getGeometryCombineMode());
260
            
261
            return x;
262
        } catch (IOException ex) {
263
            throw new RuntimeException("Can't get virtual rows", ex);
264
        }
265
    }
266
}