Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / other / localiza / LocalizaAlgorithm.java @ 59

History | View | Annotate | Download (2.84 KB)

1
package es.unex.sextante.other.localiza;
2

    
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileReader;
6
import java.io.IOException;
7
import java.util.HashMap;
8

    
9
import com.vividsolutions.jts.geom.Coordinate;
10
import com.vividsolutions.jts.geom.GeometryFactory;
11
import com.vividsolutions.jts.geom.Point;
12

    
13
import es.unex.sextante.core.GeoAlgorithm;
14
import es.unex.sextante.dataObjects.IVectorLayer;
15
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
16
import es.unex.sextante.exceptions.RepeatedParameterNameException;
17
import es.unex.sextante.outputs.OutputVectorLayer;
18

    
19
public class LocalizaAlgorithm
20
         extends
21
            GeoAlgorithm {
22

    
23
   @Override
24
   public void defineCharacteristics() {
25

    
26
      setName("localiza");
27
      setGroup("localiza");
28

    
29
      try {
30
         m_Parameters.addFilepath("FILEPATH", "filepath", true, false, false, "txt");
31
         addOutputVectorLayer("LAYER", "Resultado");
32
      }
33
      catch (final RepeatedParameterNameException e) {
34
         e.printStackTrace();
35
      }
36

    
37
   }
38

    
39

    
40
   @Override
41
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
42

    
43
      final GeometryFactory gf = new GeometryFactory();
44
      final String sFolder = m_Parameters.getParameterValueAsString("FILEPATH");
45
      final HashMap<String, Point> map = new HashMap<String, Point>();
46
      boolean bFirstLine = true;
47
      String sID = null;
48
      final Class[] types = new Class[] { String.class, String.class };
49
      final String[] sFields = new String[] { "ID", "Población" };
50
      final IVectorLayer layer = getNewVectorLayer("LAYER", "Resultado", OutputVectorLayer.SHAPE_TYPE_POINT, types, sFields);
51

    
52
      BufferedReader input;
53
      try {
54
         input = new BufferedReader(new FileReader(sFolder + File.separator + "SECCION.txt"));
55
         String sLine = null;
56
         while ((sLine = input.readLine()) != null) {
57
            final String[] sTokens = sLine.split(" ");
58
            if (bFirstLine) {
59
               sID = sTokens[0];
60
            }
61
            else {
62
               final Point pt = gf.createPoint(new Coordinate(Double.parseDouble(sTokens[1]), Double.parseDouble(sTokens[3])));
63
               map.put(sID, pt);
64
            }
65
            bFirstLine = !bFirstLine;
66
         }
67
         input.close();
68

    
69
         input = new BufferedReader(new FileReader(sFolder + File.separator + "poblacion.txt"));
70
         while ((sLine = input.readLine()) != null) {
71
            final String[] sTokens = sLine.split(" ");
72
            sID = sLine.substring(0, 3).trim();
73
            final String sPop = sLine.substring(4).trim();
74
            layer.addFeature(map.get(sID), new Object[] { sID, sPop });
75
         }
76
         input.close();
77
      }
78
      catch (final IOException e) {
79
         e.printStackTrace();
80
      }
81

    
82

    
83
      return true;
84
   }
85

    
86
}