Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. .....
  2. {
  3. var featureGeom = [];
  4. var arreglo = [];
  5. var arreglo2 = [];
  6.  
  7. var controlador = aplicacion.getController("ControlResultados");
  8. var vectorSeleccion = controlador.buscarcapa('capaSeleccion').getSource().getFeatures();
  9.  
  10. for (var x = 0; x < vectorSeleccion.length; x++) {
  11. featureGeom.push(vectorSeleccion[x].getGeometry().getCoordinates());
  12. for (var y = 0; y < featureGeom[x][0].length; y++) {
  13. arreglo.push(featureGeom[x][0][y]);
  14. }
  15. }
  16.  
  17. for (var z = 0; z < arreglo.length; z++) {
  18. arreglo2.push(arreglo[z][0]+" "+arreglo[z][1]);
  19. }
  20.  
  21. var nombreArchivo = "archivoSHPResultado";
  22. var parametros = {
  23. archivoPersistencia : nombreArchivo,
  24. geomPolygon : "POLYGON(("+arreglo2.toString()+"))",
  25. nombreArchivo: 'CapaSHPResultado'
  26. };
  27. this.descargaArchivoSHP(parametros);
  28. },
  29. descargaArchivoSHP: function(contenido){
  30. Ext.Ajax.request({
  31. url : "ServletShape",
  32. method : 'POST',
  33. params : contenido,
  34. success : function( respuesta ) {
  35. var rutaActual = location.href.split("//");
  36. var rutaActualPartes = rutaActual[1].split("/");
  37. var nuevaRuta = rutaActual[0] + "//" + rutaActualPartes[0] + "/" + rutaActualPartes[1] + "/" + respuesta.responseText;
  38.  
  39. window.open(nuevaRuta);
  40.  
  41. Ext.MessageBox.show({
  42. title: 'WARN',
  43. msg: 'OK',
  44. buttonText: {
  45. yes: 'OK',
  46. }
  47. });
  48. },
  49. failure : function() {
  50.  
  51. Ext.MessageBox.show({
  52. title: 'WARN',
  53. msg: 'FAIL',
  54. buttonText: {
  55. yes: 'OK',
  56. }
  57. });
  58. }
  59. });
  60. }
  61. ......
  62.  
  63. import java.io.File;
  64. import java.io.FileInputStream;
  65. import java.io.FileNotFoundException;
  66. import java.io.FileOutputStream;
  67. import java.io.IOException;
  68. import java.io.Serializable;
  69. import java.util.HashMap;
  70. import java.util.Map;
  71. import java.util.zip.ZipEntry;
  72. import java.util.zip.ZipOutputStream;
  73.  
  74. import org.geotools.data.DataUtilities;
  75. import org.geotools.data.DefaultTransaction;
  76. import org.geotools.data.Transaction;
  77. import org.geotools.data.shapefile.ShapefileDataStore;
  78. import org.geotools.data.shapefile.ShapefileDataStoreFactory;
  79. import org.geotools.data.simple.SimpleFeatureSource;
  80. import org.geotools.data.simple.SimpleFeatureStore;
  81. import org.geotools.feature.DefaultFeatureCollection;
  82. import org.geotools.feature.simple.SimpleFeatureBuilder;
  83. import org.geotools.referencing.crs.DefaultGeographicCRS;
  84. import org.opengis.feature.simple.SimpleFeatureType;
  85.  
  86. import utilidades.utilidadesGeometriaGeotools;
  87.  
  88. import com.vividsolutions.jts.geom.Geometry;
  89.  
  90. public class BBShape {
  91.  
  92. private static final int BUFFER = 4096;
  93. private String rutaPersistencia = null;
  94. private String nombreArchivoZip = null;
  95. private String nombreXlsSalida = null;
  96.  
  97. public String getRutaPersistencia() {
  98. return rutaPersistencia;
  99. }
  100.  
  101. public void setRutaPersistencia(String rutaPersistencia) {
  102. this.rutaPersistencia = rutaPersistencia;
  103. }
  104.  
  105. public String getNombreArchivoZip() {
  106. return nombreArchivoZip;
  107. }
  108.  
  109. public void setNombreArchivoZip(String nombreArchivoZip) {
  110. this.nombreArchivoZip = nombreArchivoZip;
  111. }
  112.  
  113. public void Exportar(String nombreShape, String tipoShape, String geomShape, String separador){
  114.  
  115. try {
  116.  
  117. String pathArchivo = rutaPersistencia + nombreShape + ".shp";
  118.  
  119. SimpleFeatureType featureType = DataUtilities.createType( tipoShape, "location:" + tipoShape + ":srid=4326," + "number:Integer");
  120.  
  121. DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal",featureType);
  122.  
  123. String[] geomArray = geomShape.split(separador);
  124.  
  125. for (int i = 0; i < geomArray.length; i++) {
  126. Geometry geometria = utilidadesGeometriaGeotools.WKTgeometriaLectura(geomArray[i]);
  127. featureCollection.add( SimpleFeatureBuilder.build( featureType, new Object[]{ geometria, 2}, null) );
  128. }
  129.  
  130. File file = new File(pathArchivo);
  131.  
  132. ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
  133.  
  134. Map<String, Serializable> params = new HashMap<String, Serializable>();
  135. params.put("url", file.toURI().toURL());
  136. params.put("create spatial index", Boolean.TRUE);
  137.  
  138. ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
  139. newDataStore.createSchema(featureType);
  140. newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
  141.  
  142. Transaction transaction = new DefaultTransaction("create");
  143.  
  144. String typeName = newDataStore.getTypeNames()[0];
  145. SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
  146.  
  147. if (featureSource instanceof SimpleFeatureStore) {
  148. SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
  149.  
  150. featureStore.setTransaction(transaction);
  151. try {
  152. featureStore.addFeatures(featureCollection);
  153. transaction.commit();
  154.  
  155. } catch (Exception problem) {
  156. problem.printStackTrace();
  157. transaction.rollback();
  158.  
  159. } finally {
  160. transaction.close();
  161. }
  162. } else {
  163. System.out.println(typeName + " No se peude acceder");
  164. }
  165.  
  166. } catch (Exception e1) {
  167.  
  168. }
  169. }
  170.  
  171. public void deleteFolderContent(File folder){
  172. File[] files = folder.listFiles();
  173. if(files!=null)
  174. for(File f: files)
  175. if(f.isDirectory()) deleteFolderContent(f);
  176. else f.delete();
  177. }
  178.  
  179. public String zipDirectorio() {
  180.  
  181. String pathCarpeta= rutaPersistencia;
  182. String pathArchivo = pathCarpeta + nombreArchivoZip + ".zip";
  183.  
  184. File d = new File(pathCarpeta);
  185. if (!d.isDirectory())
  186. throw new IllegalArgumentException(pathCarpeta + " no es un directorio." );
  187. String[] entries = d.list();
  188.  
  189. byte[] buffer = new byte[BUFFER];
  190. int bytesRead;
  191.  
  192. try {
  193. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(pathArchivo));
  194.  
  195. for (int i = 0; i < entries.length; i++) {
  196. File f = new File(d, entries[i]);
  197.  
  198. if (f.isDirectory())
  199. continue;
  200.  
  201. FileInputStream in = new FileInputStream(f);
  202.  
  203. ZipEntry entry = new ZipEntry(entries[i]);
  204. out.putNextEntry(entry);
  205.  
  206. while ((bytesRead = in.read(buffer)) != -1)
  207. out.write(buffer, 0, bytesRead);
  208. in.close();
  209. }
  210. out.close();
  211.  
  212. } catch (FileNotFoundException e) {
  213. // TODO Auto-generated catch block
  214. e.printStackTrace();
  215. } catch (IOException e) {
  216. // TODO Auto-generated catch block
  217. e.printStackTrace();
  218. }
  219.  
  220. nombreXlsSalida = this.getNombreArchivoZip() + ".zip";
  221.  
  222. return (nombreXlsSalida);
  223. }
  224.  
  225. public String exportarOpcionesSHP(String pathLogico, String ruta, String geomPolygon, String nombre){
  226.  
  227. this.setRutaPersistencia(ruta);
  228.  
  229. String strPathFile = "";
  230. File f=new File(ruta);
  231. f.mkdir();
  232.  
  233. if(f.exists()){
  234. this.deleteFolderContent(f);
  235.  
  236. if (!geomPolygon.equals("")) this.Exportar("poligonos", "Polygon", geomPolygon, "%,");
  237. }
  238.  
  239. this.setNombreArchivoZip(nombre);
  240. strPathFile = this.zipDirectorio();
  241.  
  242. strPathFile = pathLogico + strPathFile;
  243. return strPathFile;
  244. }
  245. }
  246.  
  247. package utilidades;
  248.  
  249. import java.io.IOException;
  250. import java.io.StringWriter;
  251.  
  252. import org.geotools.geometry.jts.JTS;
  253. import org.geotools.geometry.jts.JTSFactoryFinder;
  254. import org.geotools.referencing.CRS;
  255. import org.opengis.referencing.FactoryException;
  256. import org.opengis.referencing.crs.CoordinateReferenceSystem;
  257. import org.opengis.referencing.operation.MathTransform;
  258.  
  259. import com.vividsolutions.jts.geom.Geometry;
  260. import com.vividsolutions.jts.geom.GeometryFactory;
  261. import com.vividsolutions.jts.geom.LineString;
  262.  
  263. import com.vividsolutions.jts.io.ParseException;
  264. import com.vividsolutions.jts.io.WKTReader;
  265. import com.vividsolutions.jts.io.WKTWriter;
  266.  
  267. public class utilidadesGeometriaGeotools {
  268.  
  269. static public String WKTgeometria(Geometry geom){
  270.  
  271. StringWriter writer = new StringWriter();
  272. WKTWriter wktWriter = new WKTWriter(2);
  273.  
  274. try {
  275. wktWriter.write( geom, writer );
  276. } catch (IOException e) {
  277. }
  278. String wkt = writer.toString();
  279. return wkt;
  280. }
  281. static public Geometry WKTgeometriaLectura(String geom){
  282. Geometry geometria = null;
  283. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
  284. WKTReader reader = new WKTReader(geometryFactory);
  285.  
  286. try {
  287. geometria = (Geometry) reader.read(geom);
  288.  
  289. } catch (ParseException e) {
  290. // TODO Auto-generated catch block
  291. e.printStackTrace();
  292. }
  293. return geometria;
  294. }
  295.  
  296. static public LineString construirLinea (Double xIni,Double yIni,Double xFin,Double yFin ){
  297. return (LineString) WKTgeometriaLectura("LINESTRING("+xIni+" "+yIni+","+ xFin+" "+yFin+")");
  298. }
  299.  
  300. static public Geometry transformacion(Geometry geom,String sistemaOrigen,String sistemaDestino ){
  301.  
  302. Geometry salidaGeometry = null;
  303. try {
  304. CoordinateReferenceSystem sourceCRS = CRS.decode(sistemaOrigen);//CRS.decode("EPSG:4326");
  305. CoordinateReferenceSystem targetCRS = CRS.decode(sistemaDestino); //CRS.decode("EPSG:3116");
  306. MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
  307. salidaGeometry = JTS.transform( geom, transform);
  308.  
  309. } catch (Exception e) {
  310. // TODO Auto-generated catch block
  311. e.printStackTrace();
  312. }
  313. return salidaGeometry;
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement