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