PageRenderTime 46ms CodeModel.GetById 7ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/Prototipo/PrototipoMemoria/src/java/memoria/dataAccess/dao/ShapeFileDao.java

http://prototipomemoria.googlecode.com/
Java | 140 lines | 117 code | 13 blank | 10 comment | 14 complexity | 1643d98d8e93af760fd7e9c6fc08356a MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package memoria.dataAccess.dao;
  6
  7import com.vividsolutions.jts.geom.Coordinate;
  8import com.vividsolutions.jts.geom.Geometry;
  9import java.io.IOException;
 10import java.net.MalformedURLException;
 11import java.net.URL;
 12import java.util.ArrayList;
 13import java.util.List;
 14import java.util.Map;
 15import java.util.logging.Level;
 16import java.util.logging.Logger;
 17import memoria.commons.dataAccess.query.QueryParams;
 18import memoria.commons.dataAccess.query.VisualQuery;
 19import memoria.commons.entities.EntidadLinea;
 20import memoria.commons.entities.EntidadPoligono;
 21import memoria.commons.entities.EntidadPunto;
 22import memoria.commons.entities.Municipio;
 23import memoria.commons.structures.GeoReferenced;
 24import memoria.commons.structures.Line;
 25import memoria.commons.structures.MultiPolygon;
 26import memoria.commons.structures.Point;
 27import memoria.commons.structures.Polygon;
 28import memoria.commons.structures.coordinates.LatLonCoordinate;
 29import memoria.dataAccess.IRepositoryDao;
 30import memoria.dataAccess.SpatialDTO;
 31import org.geotools.data.shapefile.ShapefileDataStore;
 32import org.geotools.data.shapefile.ShapefileDataStoreFactory;
 33import org.geotools.data.simple.SimpleFeatureCollection;
 34import org.geotools.data.simple.SimpleFeatureIterator;
 35import org.opengis.feature.simple.SimpleFeature;
 36
 37/**
 38 *
 39 * @author diego
 40 */
 41public class ShapeFileDao implements IRepositoryDao {
 42
 43    //Patron Singleton
 44    private static ShapeFileDao instance;
 45
 46    public static IRepositoryDao getInstance() {
 47        if (instance == null) {
 48            instance = new ShapeFileDao();
 49        }
 50        return instance;
 51    }
 52
 53    private String fileUrl;
 54
 55    public List<GeoReferenced> getData(VisualQuery params) {
 56        List<GeoReferenced> results = new ArrayList<GeoReferenced>();
 57        try {
 58            //ShapefileDataStore shp = new ShapefileDataStore(new URL("file://"+this.getClass().getResource("/memoria/resources/data/comisarias/comisarias_reprojected.shp").getPath()));
 59            ShapefileDataStore shp = new ShapefileDataStore(new URL("file://" + this.fileUrl));
 60            SimpleFeatureCollection col = shp.getFeatureSource().getFeatures();
 61            SimpleFeatureIterator iterator = col.features();
 62            try {
 63                while (iterator.hasNext()) {
 64                    SimpleFeature feature = iterator.next();
 65                    try {
 66                        results.add(instanciarEntidad(feature));
 67                    } catch (Exception e) {
 68                        Logger.getLogger(ShapeFileDao.class.getName()).log(Level.SEVERE, "Error al instanciar entidad del shapefile", e);
 69                    }
 70                }
 71            } finally {
 72                iterator.close(); // IMPORTANT
 73            }
 74
 75        } catch (Exception ex) {
 76            Logger.getLogger(ShapeFileDao.class.getName()).log(Level.SEVERE, "Error al leer ShapeFile", ex);
 77        }
 78        if (params.getFiltro() != null) {
 79            return params.getFiltro().filter(results);
 80        }
 81        return results;
 82    }
 83
 84    public GeoReferenced instanciarEntidad(SimpleFeature feature) {
 85        Geometry geom = (Geometry) feature.getDefaultGeometry();
 86        GeoReferenced entidad = null;
 87        if (geom.getGeometryType().equals("Point")) {
 88            LatLonCoordinate coordenadas = new LatLonCoordinate(geom.getCoordinate().y, geom.getCoordinate().x);
 89            Point punto = new Point(coordenadas);
 90            entidad = new EntidadPunto(feature.getAttribute(1).toString(), feature.getAttribute(2).toString(), punto);
 91        }
 92        if (geom.getGeometryType().equals("Polygon")) {
 93            for (Coordinate coordenada : geom.getCoordinates()) {
 94                LatLonCoordinate coordenadas = new LatLonCoordinate(coordenada.y, coordenada.x, coordenada.z);
 95                Point punto = new Point(coordenadas);
 96                entidad = new EntidadPunto(feature.getAttribute(1).toString(), feature.getAttribute(2).toString(), punto);
 97            }
 98        }
 99        if (geom.getGeometryType().equals("MultiLineString")) {
100            Line line = new Line();
101            line.setCenter(new Point(new LatLonCoordinate(geom.getCentroid().getCoordinate().y, geom.getCentroid().getCoordinate().x)));
102            for (Coordinate coordenada : geom.getCoordinates()) {
103                LatLonCoordinate coordenadas = new LatLonCoordinate(coordenada.y, coordenada.x, coordenada.z);
104                Point punto = new Point(coordenadas);
105                line.getPoints().add(punto);                
106            }
107            entidad = new EntidadLinea(feature.getAttribute(1).toString(), "", line);
108        }
109
110        
111        if (geom.getGeometryType().equals("MultiPolygon")) {
112            MultiPolygon multiPolygon = new MultiPolygon(null);
113            int numberOfGeometries = geom.getNumGeometries();
114            for (int i = 0; i < numberOfGeometries; i++) {
115                Geometry geomN = geom.getGeometryN(i);
116                if (geomN.getGeometryType().equals("Polygon")) {
117                   Polygon polygon = new Polygon(new Point(new LatLonCoordinate(geomN.getCentroid().getY(),geomN.getCentroid().getX())));
118                    for (Coordinate coordenada : geomN.getCoordinates()) {
119                        LatLonCoordinate coordenadas = new LatLonCoordinate(coordenada.y, coordenada.x, coordenada.z);
120                        Point punto = new Point(coordenadas);
121                        polygon.getPoints().add(punto);
122                    }
123                    multiPolygon.getPoligonos().add(polygon);
124                }
125            }
126            EntidadPoligono entidadPoligono = new EntidadPoligono();
127            entidadPoligono.setLocation(multiPolygon.getPoligonos().get(0));
128            entidadPoligono.setNombre(feature.getAttribute(1).toString());
129            entidadPoligono.setDescripcion(feature.getAttribute(2).toString());
130            entidad = entidadPoligono;
131            
132        }
133    return entidad ;
134}
135
136    public void setInitParams(Map<String, String> params) {
137        this.fileUrl = params.get("URL");
138    }
139
140}