Revision 4820 trunk/extensions/extGeoProcessing/src/com/iver/gvsig/geoprocessing/gui/GeoProcessingPanel.java

View differences:

GeoProcessingPanel.java
45 45

  
46 46
import java.awt.CardLayout;
47 47
import java.awt.Component;
48
import java.awt.Container;
49
import java.awt.Window;
48 50
import java.io.File;
49
import java.io.IOException;
50 51
import java.util.HashMap;
51 52
import java.util.Map;
52 53

  
53 54
import javax.swing.JButton;
54
import javax.swing.JDialog;
55
import javax.swing.JOptionPane;
56 55
import javax.swing.JPanel;
57 56

  
58 57
import org.cresques.cts.IProjection;
......
60 59
import com.iver.andami.PluginServices;
61 60
import com.iver.andami.ui.mdiManager.View;
62 61
import com.iver.andami.ui.mdiManager.ViewInfo;
63
import com.iver.cit.gvsig.fmap.DriverException;
64 62
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
65
import com.iver.cit.gvsig.fmap.edition.EditionException;
66 63
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
67 64
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
68
import com.iver.cit.gvsig.fmap.layers.CancelationException;
69 65
import com.iver.cit.gvsig.fmap.layers.FLayers;
70 66
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
67
import com.iver.gvsig.geoprocessing.AddResultLayerTask;
68
import com.iver.gvsig.geoprocessing.BufferGeoprocessController;
69
import com.iver.gvsig.geoprocessing.ClipGeoprocessController;
70
import com.iver.gvsig.geoprocessing.ConvexHullGeoprocessController;
71
import com.iver.gvsig.geoprocessing.DifferenceGeoprocessController;
72
import com.iver.gvsig.geoprocessing.DissolveGeoprocessController;
73
import com.iver.gvsig.geoprocessing.IntersectionGeoprocessController;
74
import com.iver.gvsig.geoprocessing.MergeGeoprocessController;
75
import com.iver.gvsig.geoprocessing.SpatialJoinGeoprocessController;
76
import com.iver.gvsig.geoprocessing.UnionGeoprocessController;
71 77
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingBufferPanel;
72 78
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingConvexHullPanel;
73 79
import com.iver.gvsig.geoprocessing.gui.operationpanels.GeoProcessingDissolvePanel;
......
85 91
import com.iver.gvsig.geoprocessing.impl.spatialjoin.SpatialJoinGeoprocess;
86 92
import com.iver.gvsig.geoprocessing.impl.union.UnionGeoprocess;
87 93
import com.iver.gvsig.geoprocessing.model.GeoprocessException;
88
import com.iver.gvsig.geoprocessing.model.IGeoprocess;
89 94
import com.iver.utiles.swing.threads.IMonitorableTask;
90 95
import com.iver.utiles.swing.threads.MonitorableDecorator;
91 96

  
......
154 159

  
155 160
	private JPanel mainPanel = null;
156 161
	
162
	/**
163
	 * These String constants are used to tells to CardLayout which
164
	 * panel it must show, in response to user selection of radio button
165
	 * panel.
166
	 */
157 167
	private final static String CONVEX_HULL = "convex";
158 168
	private final static String SPATIAL_JOIN = "spt_join";
159 169
	private final static String UNION = "union";
......
165 175
	private final static String BUFFER = "buffer";
166 176
	private final static String OP_SELECT = "opselect";
167 177
	
168

  
169 178
	/**
170
	 * This constructor initializes the set of layers
179
	 * Constructor
180
	 * @param layers
181
	 * @param proj
171 182
	 */
172 183
	public GeoProcessingPanel(FLayers layers, IProjection proj) {
173 184
		super();
......
182 193
	 */
183 194
	public void closeDialog() {
184 195
		if (PluginServices.getMainFrame() == null) {
185
			// TODO Azabala: ni idea de porque 4 llamadas a getParent()
186
			((JDialog) (getParent().getParent().getParent().getParent()))
187
					.dispose();
196
			Container container = getParent();
197
			Container parentOfContainer = null;
198
			//TODO This code is used in many classes
199
			//Reuse it
200
			while(! (container instanceof Window)){
201
				parentOfContainer = container.getParent();
202
				container = parentOfContainer;
203
			}
204
			((Window)container).dispose();	
188 205
		} else {
189 206
			PluginServices.getMDIManager().closeView(GeoProcessingPanel.this);
190 207
		}
......
305 322
	}
306 323

  
307 324
	private void showConvexHullPanel() {
308
		getGeoProcessingOperationSelectorPanel().setVisible(false);
309
		getGeoProcessingBufferPanel().setVisible(false);
310
		getGeoProcessingClipPanel().setVisible(false);
311
		getGeoProcessingDissolvePanel().setVisible(false);
312
		getGeoProcessingMergePanel().setVisible(false);
313
		getGeoProcessingIntersectPanel().setVisible(false);
314
		getGeoProcessingUnionPanel().setVisible(false);
315
		getGeoProcessingSpatialjoinPanel().setVisible(false);
316
		getGeoProcessingDifferencePanel().setVisible(false);
317
		getGeoProcessingConvexHullPanel().setVisible(true);
325
		((CardLayout)mainPanel.getLayout()).show(mainPanel, CONVEX_HULL);
318 326
	}
319 327

  
320 328
	private Component getGeoProcessingConvexHullPanel() {
......
335 343
	 * @return void
336 344
	 */
337 345
	private void initialize() {
338
		//this.setLayout(new BorderLayout());
339 346
		this.setLayout(null);
340 347
		this.setSize(500, 400);
341 348
		this.add(getButtonsPanel(), null);
342 349
		this.add(getMainPanel(), null);
343
		geoProcessingOperationSelectorPanel.setVisible(true);
344
		geoProcessingBufferPanel.setVisible(false);
345
		geoProcessingClipPanel.setVisible(false);
346
		geoProcessingDissolvePanel.setVisible(false);
347
		geoProcessingMergePanel.setVisible(false);
348
		geoProcessingIntersectPanel.setVisible(false);
349
		geoProcessingUnionPanel.setVisible(false);
350
		geoProcessingSpatialjoinPanel.setVisible(false);
351
		geoProcessingDifferencePanel.setVisible(false);
352
		geoProcessingConvexHullPanel.setVisible(false);
353

  
354
		// esto va contra la legibilidad del codigo
350
		showOptionSelectionPanel();
355 351
		previousButton.setEnabled(false);
356 352
	}
357 353

  
......
579 575
	}
580 576

  
581 577
	public boolean doBuffer() {
582
		FLyrVect inputLayer = geoProcessingBufferPanel.getInputLayer();
583
		File outputFile = geoProcessingBufferPanel.getOutputFile();
584
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
585
			String error = PluginServices.getText(this, "Error_entrada_datos");
586
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
587
			error(errorDescription, error);
588
			return false;
589
		}
590
		final BufferGeoprocess buffer = new BufferGeoprocess(inputLayer);
591
		// TODO Hay que desacoplar el SchemaManager y el
592
		// LayerDefinition del formato Shp, as? como del
593
		// uso de ficheros
594
		SHPLayerDefinition definition = (SHPLayerDefinition) buffer
595
				.createLayerDefinition();
596
		definition.setFile(outputFile);
597
		ShpSchemaManager schemaManager = new ShpSchemaManager();
598
		ShpWriter writer = null;
599
		try {
600
			writer = getShpWriter(definition);
601
		} catch (Exception e1) {
602
			String error = PluginServices.getText(this, "Error_escritura_resultados");
603
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
604
			error(errorDescription, error);
605
			return false;
606
		} 
607
		buffer.setResultLayerProperties(writer, schemaManager);
608
		HashMap params = new HashMap();
609
		boolean onlySelected = geoProcessingBufferPanel.isBufferOnlySelected();
610
		params.put("layer_selection", new Boolean(onlySelected));
611
		boolean dissolveBuffer = geoProcessingBufferPanel
612
				.isDissolveBuffersSelected();
613
		params.put("dissolve_buffers", new Boolean(dissolveBuffer));
614
		byte strategy = 0;
615
		if (geoProcessingBufferPanel.isConstantDistanceSelected()) {
616
			strategy = BufferGeoprocess.CONSTANT_DISTANCE_STRATEGY;
617
			double bufferDistance = -1;
618
			try {
619
				bufferDistance = geoProcessingBufferPanel.getConstantDistance();
620
			} catch (GeoprocessException e) {
621
				String error = PluginServices.getText(this, "Error_entrada_datos");
622
				String errorDescription = PluginServices.getText(this, "Error_distancia_buffer");
623
				error(errorDescription, error);
624
				return false;
625
			}
626
			params.put("buffer_distance", new Double(bufferDistance));
627
		} else if (geoProcessingBufferPanel.isAttributeDistanceSelected()) {
628
			strategy = BufferGeoprocess.ATTRIBUTE_DISTANCE_STRATEGY;
629
			String attributeName = null;
630
			try {
631
				attributeName = geoProcessingBufferPanel
632
						.getAttributeDistanceField();
633
			} catch (GeoprocessException e) {
634
				String error = PluginServices.getText(this, "Error_entrada_datos");
635
				String errorDescription = PluginServices.getText(this, "Error_atributo_no_numerico");
636
				error(errorDescription, error);
637
				return false;
638
			}
639
			params.put("attr_name", attributeName);
640
		}
641
		params.put("strategy_flag", new Byte(strategy));
642
		try {
643
			buffer.setParameters(params);
644
			buffer.checkPreconditions();
645
			IMonitorableTask task1 = buffer.createTask();
646
			IMonitorableTask task2 = new AddResultLayerTask(buffer);
647
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
648
					task2);
649
			if (globalTask.preprocess())
650
				PluginServices.cancelableBackgroundExecution(globalTask);
651
		} catch (GeoprocessException e) {
652
			String error = PluginServices.getText(this, "Error_ejecucion");
653
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
654
			error(errorDescription, error);
655
			return false;
656
		}
657
		return true;
578
		BufferGeoprocessController controller =
579
			new BufferGeoprocessController();
580
		controller.setView(geoProcessingBufferPanel);
581
		return controller.launchGeoprocess();
658 582
	}
659 583

  
660 584
	private ShpWriter getShpWriter(SHPLayerDefinition definition) throws Exception {
661
		ShpWriter writer = null;
662
		writer = new ShpWriter();
585
		ShpWriter writer = new ShpWriter();
663 586
		writer.setFile(definition.getFile());
664 587
		writer.initialize(definition);
665 588
		return writer;
666 589
	}
667 590

  
668 591
	public boolean doMerge() {
669
		FLyrVect[] inputLayers = geoProcessingMergePanel.getSelectedLayers();
670
		FLyrVect schemaToPreserve = geoProcessingMergePanel.getSelectedSchema();
671
		File outputFile = geoProcessingMergePanel.getOutputFile();
672
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
673
			String error = PluginServices.getText(this, "Error_entrada_datos");
674
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
675
			error(errorDescription, error);
676
			return false;
677
		}
678
		MergeGeoprocess merge = new MergeGeoprocess();
679
		merge.setInputLayers(inputLayers);
680
		merge.setOutputSchemaLayer(schemaToPreserve);
681
		
682
		SHPLayerDefinition definition = (SHPLayerDefinition) merge
683
										.createLayerDefinition();
684
		definition.setFile(outputFile);
685
		ShpSchemaManager schemaManager = new ShpSchemaManager();
686
		ShpWriter writer = null;
687
		try {
688
			schemaManager.createOrAlterSchema(definition);
689
			writer = new ShpWriter();
690
			writer.setFile(outputFile);
691
			writer.initialize(definition);
692
		} catch (Exception e) {
693
			String error = PluginServices.getText(this, "Error_escritura_resultados");
694
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
695
			error(errorDescription, error);
696
			return false;
697
		}
698
		merge.setResultLayerProperties(writer, schemaManager);
699
		try {
700
			merge.checkPreconditions();
701
		} catch (GeoprocessException e) {
702
			String error = PluginServices.getText(this, "Error_chequeando_precondiciones");
703
			String errorDescription = PluginServices.getText(this, "Error_chequeo_tipo_geometria");
704
			error(errorDescription, error);
705
			return false;
706
		}
707
		IMonitorableTask task1 = merge.createTask();
708
		IMonitorableTask task2 = new AddResultLayerTask(merge);
709
		MonitorableDecorator globalTask = new MonitorableDecorator(task1,
710
				task2);
711
		if (globalTask.preprocess())
712
			PluginServices.cancelableBackgroundExecution(globalTask);
713
		return true;
592
		MergeGeoprocessController controller =
593
			new MergeGeoprocessController();
594
		controller.setView(geoProcessingMergePanel);
595
		return controller.launchGeoprocess();
714 596
	}
715 597

  
716 598
	public boolean doDissolve() {
717
		FLyrVect inputLayer = geoProcessingDissolvePanel.getInputLayer();
718
		File outputFile = geoProcessingDissolvePanel.getOutputFile();
719
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
720
			String error = PluginServices.getText(this, "Error_entrada_datos");
721
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
722
			error(errorDescription, error);
723
			return false;
724
		}
725
		String dissolveField = geoProcessingDissolvePanel
726
				.getDissolveFieldName();
727
		boolean onlySelection = geoProcessingDissolvePanel
728
				.isDissolveOnlySelected();
729
		DissolveGeoprocess dissolve = new DissolveGeoprocess(inputLayer,
730
				dissolveField);
731

  
732
		// Antes de crear el esquema vemos la seleccion del usuario
733
		Map fields_functions = geoProcessingDissolvePanel.getFieldFunctionMap();
734
		dissolve.setFieldsFunctions(fields_functions);
735

  
736
		SHPLayerDefinition definition = (SHPLayerDefinition) dissolve
737
				.createLayerDefinition();
738
		definition.setFile(outputFile);
739
		ShpSchemaManager schemaManager = new ShpSchemaManager();
740
		
741
		ShpWriter writer = null;
742
		try {
743
			schemaManager.createOrAlterSchema(definition);
744
			writer = new ShpWriter();
745
			writer.setFile(outputFile);
746
			writer.initialize(definition);
747
		} catch (Exception e) {
748
			String error = PluginServices.getText(this, "Error_escritura_resultados");
749
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
750
			error(errorDescription, error);
751
			return false;
752
		} 
753
		dissolve.setResultLayerProperties(writer, schemaManager);
754
		HashMap params = new HashMap();
755
		params.put("layer_selection", new Boolean(onlySelection));
756
		try {
757
			dissolve.setParameters(params);
758
			dissolve.checkPreconditions();
759
			IMonitorableTask task1 = dissolve.createTask();
760
			IMonitorableTask task2 = new AddResultLayerTask(dissolve);
761
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
762
					task2);
763
			if (globalTask.preprocess())
764
				PluginServices.cancelableBackgroundExecution(globalTask);
765
		} catch (GeoprocessException e) {
766
			String error = PluginServices.getText(this, "Error_ejecucion");
767
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
768
			error(errorDescription, error);
769
			return false;
770
		}
771
		return true;
772
		
599
		DissolveGeoprocessController controller =
600
			new DissolveGeoprocessController();
601
		controller.setView(geoProcessingDissolvePanel);
602
		return controller.launchGeoprocess();
773 603
	}
774 604

  
775 605
	public boolean doSpatialJoin() {
776
		// this open a modal dialog and sets sumarize functions
777
		Map sumarizeFunctions = null;
778
		if (!geoProcessingSpatialjoinPanel.isNearestSelected()){
779
			boolean isOk = geoProcessingSpatialjoinPanel.openSumarizeFunction();
780
			if(! isOk){
781
				//Ok button wasnt pushed. Dialog was closed directly
782
				return false;
783
			}
784
			sumarizeFunctions = geoProcessingSpatialjoinPanel
785
				.getSumarizeFunctions();
786
			if(sumarizeFunctions == null || sumarizeFunctions.size() == 0){
787
				error(PluginServices.getText(this, "Error_spjoinmn_sin_funcion"),
788
				PluginServices.getText(this, "Error_entrada_datos"));
789
			}
790
		}
791
		FLyrVect inputLayer = geoProcessingSpatialjoinPanel.getFirstLayer();
792
		FLyrVect secondLayer = geoProcessingSpatialjoinPanel.getSecondLayer();
606
		SpatialJoinGeoprocessController controller =
607
			new SpatialJoinGeoprocessController();
608
		controller.setView(geoProcessingSpatialjoinPanel);
609
		return controller.launchGeoprocess();
793 610

  
794
		File outputFile = geoProcessingSpatialjoinPanel.getOutputFile();
795
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
796
			String error = PluginServices.getText(this, "Error_entrada_datos");
797
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
798
			error(errorDescription, error);
799
			return false;
800
		}
801
		boolean onlyFirstSelection = geoProcessingSpatialjoinPanel
802
				.onlyFirstLayerSelected();
803
		boolean onlySecondSelection = geoProcessingSpatialjoinPanel
804
				.onlySecondLayerSelected();
805
		boolean nearest = geoProcessingSpatialjoinPanel.isNearestSelected();
806
		SpatialJoinGeoprocess join = new SpatialJoinGeoprocess(inputLayer);
807
		join.setSecondOperand(secondLayer);
808
		join.setFields_sumFunctions(sumarizeFunctions);
809
		HashMap params = new HashMap();
810
		Boolean first = new Boolean(onlyFirstSelection);
811
		params.put("firstlayerselection", first);
812

  
813
		Boolean second = new Boolean(onlySecondSelection);
814
		params.put("secondlayerselection", second);
815

  
816
		Boolean nearSel = new Boolean(nearest);
817
		params.put("nearest", nearSel);
818
		try {
819
			ShpSchemaManager schemaManager = new ShpSchemaManager();
820
			ShpWriter writer = new ShpWriter();
821
			join.setResultLayerProperties(writer, schemaManager);
822
			join.setParameters(params);
823
			SHPLayerDefinition definition = 
824
				(SHPLayerDefinition) join.createLayerDefinition();
825
			definition.setFile(outputFile);
826
			writer.setFile(definition.getFile());
827
			writer.initialize(definition);
828
			writer = getShpWriter(definition);
829
			join.checkPreconditions();
830
			IMonitorableTask task1 = join.createTask();
831
			if(task1 == null){
832
				//mensaje de error
833
				return false;
834
			}
835
			IMonitorableTask task2 = new AddResultLayerTask(join);
836
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
837
					task2);
838
			if (globalTask.preprocess())
839
				PluginServices.cancelableBackgroundExecution(globalTask);
840
		} catch (GeoprocessException e) {
841
			String error = PluginServices.getText(this, "Error_ejecucion");
842
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
843
			error(errorDescription, error);
844
			return false;
845

  
846
		} catch (Exception e) {
847
			String error = PluginServices.getText(this, "Error_escritura_resultados");
848
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
849
			error(errorDescription, error);
850
			return false;
851
		}
852
		return true;
853

  
854 611
	}
855 612

  
856
	/**
857
	 * Shows an alert dialog
858
	 * 
859
	 * @param message
860
	 *            body of message dialog
861
	 * @param title
862
	 *            title of mesage dialog
863
	 * 
864
	 * TODO Mirar si es mejor utilizar el componente generico de ANDAMI
865
	 * (notification dialog)
866
	 */
867
	private void error(String message, String title) {
868
		JOptionPane.showMessageDialog(this, message, title,
869
				JOptionPane.ERROR_MESSAGE);
870
	}
871

  
872 613
	public boolean doClip() {
873
		FLyrVect inputLayer = geoProcessingClipPanel.getInputLayer();
874
		FLyrVect clippingLayer = geoProcessingClipPanel.getSecondLayer();
875
		File outputFile = geoProcessingClipPanel.getOutputFile();
876
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
877
			String error = PluginServices.getText(this, "Error_entrada_datos");
878
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
879
			error(errorDescription, error);
880
			return false;
881
		}
882
		ClipGeoprocess clip = new ClipGeoprocess(inputLayer);
883
		clip.setSecondOperand(clippingLayer);
884
		SHPLayerDefinition definition = (SHPLayerDefinition) clip
885
				.createLayerDefinition();
886
		definition.setFile(outputFile);
887
		ShpSchemaManager schemaManager = new ShpSchemaManager();
888
		ShpWriter writer = null;
889
		try {
890
			writer = getShpWriter(definition);
891
		} catch (Exception e1) {
892
			String error = PluginServices.getText(this, "Error_escritura_resultados");
893
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
894
			error(errorDescription, error);
895
			return false;
896
		} 
897
		clip.setResultLayerProperties(writer, schemaManager);
898
		HashMap params = new HashMap();
899
		boolean onlySelectedFirst = geoProcessingClipPanel
900
				.onlyFirstLayerSelected();
901
		boolean onlySelectedSecond = geoProcessingClipPanel
902
				.onlySecondLayerSelected();
903
		Boolean first = new Boolean(onlySelectedFirst);
904
		params.put("firstlayerselection", first);
905

  
906
		Boolean second = new Boolean(onlySelectedSecond);
907
		params.put("secondlayerselection", second);
908

  
909
		try {
910
			clip.setParameters(params);
911
			clip.checkPreconditions();
912
			IMonitorableTask task1 = clip.createTask();
913
			IMonitorableTask task2 = new AddResultLayerTask(clip);
914
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
915
					task2);
916
			if (globalTask.preprocess())
917
				PluginServices.cancelableBackgroundExecution(globalTask);
918
		} catch (GeoprocessException e) {
919
			String error = PluginServices.getText(this, "Error_ejecucion");
920
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
921
			error(errorDescription, error);
922
			return false;
923
		}
924
		return true;
614
		ClipGeoprocessController controller =
615
			new ClipGeoprocessController();
616
		controller.setView(geoProcessingClipPanel);
617
		return controller.launchGeoprocess();
925 618
	}
926 619
	
927 620
	// Spatial join con Intersect
928 621
	public boolean doIntersect() {
929
		FLyrVect inputLayer = geoProcessingIntersectPanel.getInputLayer();
930
		FLyrVect overlayLayer = geoProcessingIntersectPanel.getSecondLayer();
931
		File outputFile = geoProcessingIntersectPanel.getOutputFile();
932
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
933
			String error = PluginServices.getText(this, "Error_entrada_datos");
934
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
935
			error(errorDescription, error);
936
			return false;
937
		}
938

  
939
		IntersectionGeoprocess intersection = new IntersectionGeoprocess(
940
				inputLayer);
941
		intersection.setSecondOperand(overlayLayer);
942
		SHPLayerDefinition definition = (SHPLayerDefinition) intersection
943
				.createLayerDefinition();
944
		definition.setFile(outputFile);
945
		ShpSchemaManager schemaManager = new ShpSchemaManager();
946
		ShpWriter writer;
947
		try {
948
			writer = getShpWriter(definition);
949
		} catch (Exception e1) {
950
			String error = PluginServices.getText(this, "Error_escritura_resultados");
951
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
952
			error(errorDescription, error);
953
			return false;
954
		} 
955
	
956
		intersection.setResultLayerProperties(writer, schemaManager);
957
		HashMap params = new HashMap();
958
		boolean onlySelectedFirst = geoProcessingIntersectPanel
959
				.onlyFirstLayerSelected();
960
		boolean onlySelectedSecond = geoProcessingIntersectPanel
961
				.onlySecondLayerSelected();
962
		Boolean first = new Boolean(onlySelectedFirst);
963
		params.put("firstlayerselection", first);
964

  
965
		Boolean second = new Boolean(onlySelectedSecond);
966
		params.put("secondlayerselection", second);
967

  
968
		try {
969
			intersection.setParameters(params);
970
			intersection.checkPreconditions();
971
			IMonitorableTask task1 = intersection.createTask();
972
			if(task1 == null){
973
				return false;
974
			}
975
			IMonitorableTask task2 = new AddResultLayerTask(intersection);
976
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
977
					task2);
978
			if (globalTask.preprocess())
979
				PluginServices.cancelableBackgroundExecution(globalTask);
980
		} catch (GeoprocessException e) {
981
			String error = PluginServices.getText(this, "Error_ejecucion");
982
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
983
			error(errorDescription, error);
984
			return false;
985
		}
986
		return true;
622
		IntersectionGeoprocessController controller =
623
			new IntersectionGeoprocessController();
624
		controller.setView(geoProcessingIntersectPanel);
625
		return controller.launchGeoprocess();
987 626
	}
988 627

  
989 628
	public boolean doUnion() {
990
		FLyrVect inputLayer = geoProcessingUnionPanel.getInputLayer();
991
		FLyrVect overlayLayer = geoProcessingUnionPanel.getSecondLayer();
992
		File outputFile = geoProcessingUnionPanel.getOutputFile();
993
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
994
			String error = PluginServices.getText(this, "Error_entrada_datos");
995
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
996
			error(errorDescription, error);
997
			return false;
998
		}
999
		UnionGeoprocess union = new UnionGeoprocess(inputLayer);
1000
		union.setSecondOperand(overlayLayer);
1001
		SHPLayerDefinition definition = (SHPLayerDefinition) union
1002
				.createLayerDefinition();
1003
		definition.setFile(outputFile);
1004
		ShpSchemaManager schemaManager = new ShpSchemaManager();
1005
		ShpWriter writer = null;
1006
		try {
1007
			writer = getShpWriter(definition);
1008
		} catch (Exception e1) {
1009
			String error = PluginServices.getText(this, "Error_escritura_resultados");
1010
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
1011
			error(errorDescription, error);
1012
			return false;
1013
		} 
1014
		union.setResultLayerProperties(writer, schemaManager);
1015
		HashMap params = new HashMap();
1016
		boolean onlySelectedFirst = geoProcessingUnionPanel
1017
				.onlyFirstLayerSelected();
1018
		boolean onlySelectedSecond = geoProcessingUnionPanel
1019
				.onlySecondLayerSelected();
1020
		Boolean first = new Boolean(onlySelectedFirst);
1021
		params.put("firstlayerselection", first);
1022

  
1023
		Boolean second = new Boolean(onlySelectedSecond);
1024
		params.put("secondlayerselection", second);
1025

  
1026
		try {
1027
			union.setParameters(params);
1028
			union.checkPreconditions();
1029
			IMonitorableTask task1 = union.createTask();
1030
			if(task1 == null){
1031
				return false;
1032
			}
1033
			IMonitorableTask task2 = new AddResultLayerTask(union);
1034
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
1035
					task2);
1036
			if (globalTask.preprocess())
1037
				PluginServices.cancelableBackgroundExecution(globalTask);
1038
		} catch (GeoprocessException e) {
1039
			String error = PluginServices.getText(this, "Error_ejecucion");
1040
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
1041
			error(errorDescription, error);
1042
			return false;
1043
		}
1044
		return true;
629
		UnionGeoprocessController controller =
630
			new UnionGeoprocessController();
631
		controller.setView(geoProcessingUnionPanel);
632
		return controller.launchGeoprocess();
1045 633
	}
1046 634

  
1047 635
	
1048 636
	public boolean doConvexHull() {
1049
		FLyrVect inputLayer = geoProcessingConvexHullPanel.getInputLayer();
1050
		File outputFile = geoProcessingConvexHullPanel.getOutputFile();
1051
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
1052
			String error = PluginServices.getText(this, "Error_entrada_datos");
1053
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
1054
			error(errorDescription, error);
1055
			return false;
1056
		}
1057
		ConvexHullGeoprocess convexHull = new ConvexHullGeoprocess();
1058
		convexHull.setFirstOperand(inputLayer);
1059

  
1060
		SHPLayerDefinition definition = (SHPLayerDefinition) convexHull
1061
				.createLayerDefinition();
1062
		definition.setFile(outputFile);
1063
		ShpSchemaManager schemaManager = new ShpSchemaManager();
1064
		ShpWriter writer = null;
1065
		try {
1066
			writer = getShpWriter(definition);
1067
		} catch (Exception e1) {
1068
			String error = PluginServices.getText(this, "Error_escritura_resultados");
1069
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
1070
			error(errorDescription, error);
1071
			return false;
1072
		} 
1073
		convexHull.setResultLayerProperties(writer, schemaManager);
1074
		HashMap params = new HashMap();
1075
		boolean onlySelected = geoProcessingConvexHullPanel
1076
				.isConvexHullOnlySelected();
1077
		params.put("layer_selection", new Boolean(onlySelected));
1078
		try {
1079
			convexHull.setParameters(params);
1080
			convexHull.checkPreconditions();
1081
			IMonitorableTask task1 = convexHull.createTask();
1082
			if(task1 == null){
1083
				return false;
1084
				
1085
			}
1086
			IMonitorableTask task2 = new AddResultLayerTask(convexHull);
1087
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
1088
					task2);
1089
			if (globalTask.preprocess())
1090
				PluginServices.cancelableBackgroundExecution(globalTask);
1091
			
1092
		} catch (GeoprocessException e) {
1093
			String error = PluginServices.getText(this, "Error_ejecucion");
1094
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
1095
			error(errorDescription, error);
1096
			return false;
1097
		}
1098
		return true;
637
		ConvexHullGeoprocessController controller =
638
			new ConvexHullGeoprocessController();
639
		controller.setView(geoProcessingConvexHullPanel);
640
		return controller.launchGeoprocess();
1099 641
	}
1100 642

  
1101 643
	public boolean doDifference() {
1102
		FLyrVect inputLayer = geoProcessingDifferencePanel.getInputLayer();
1103
		FLyrVect overlayLayer = geoProcessingDifferencePanel.getSecondLayer();
1104
		File outputFile = geoProcessingDifferencePanel.getOutputFile();
1105
		if (outputFile == null || (outputFile.getAbsolutePath().length() == 0)) {
1106
			String error = PluginServices.getText(this, "Error_entrada_datos");
1107
			String errorDescription = PluginServices.getText(this, "Error_seleccionar_resultado");
1108
			error(errorDescription, error);
1109
			return false;
1110
		}
1111
		DifferenceGeoprocess diff = new DifferenceGeoprocess(inputLayer);
1112
		diff.setSecondOperand(overlayLayer);
1113
		SHPLayerDefinition definition = (SHPLayerDefinition) diff
1114
				.createLayerDefinition();
1115
		definition.setFile(outputFile);
1116
		ShpSchemaManager schemaManager = new ShpSchemaManager();
1117
		ShpWriter writer = null;
1118
		try {
1119
			writer = getShpWriter(definition);
1120
		} catch (Exception e1) {
1121
			String error = PluginServices.getText(this, "Error_escritura_resultados");
1122
			String errorDescription = PluginServices.getText(this, "Error_preparar_escritura_resultados");
1123
			error(errorDescription, error);
1124
			return false;
1125
		} 
1126
		diff.setResultLayerProperties(writer, schemaManager);
1127
		HashMap params = new HashMap();
1128
		boolean onlySelectedFirst = geoProcessingDifferencePanel
1129
				.onlyFirstLayerSelected();
1130
		boolean onlySelectedSecond = geoProcessingDifferencePanel
1131
				.onlySecondLayerSelected();
1132
		Boolean first = new Boolean(onlySelectedFirst);
1133
		params.put("firstlayerselection", first);
1134

  
1135
		Boolean second = new Boolean(onlySelectedSecond);
1136
		params.put("secondlayerselection", second);
1137

  
1138
		try {
1139
			diff.setParameters(params);
1140
			diff.checkPreconditions();
1141
			
1142
			IMonitorableTask task1 = diff.createTask();
1143
			if(task1 == null){
1144
				return false;
1145
				
1146
			}
1147
			IMonitorableTask task2 = new AddResultLayerTask(diff);
1148
			MonitorableDecorator globalTask = new MonitorableDecorator(task1,
1149
					task2);
1150
			if (globalTask.preprocess())
1151
				PluginServices.cancelableBackgroundExecution(globalTask);
1152
			
1153
		} catch (GeoprocessException e) {
1154
			String error = PluginServices.getText(this, "Error_ejecucion");
1155
			String errorDescription = PluginServices.getText(this, "Error_fallo_geoproceso");
1156
			error(errorDescription, error);
1157
			return false;
1158
		}
1159
		return true;
644
		DifferenceGeoprocessController controller =
645
			new DifferenceGeoprocessController();
646
		controller.setView(geoProcessingDifferencePanel);
647
		return controller.launchGeoprocess();
1160 648
	}
1161 649

  
1162
	/**
1163
	 * Implementation of IMonitorableTask that add result layer
1164
	 * of a geoprocess to the TOC in a background thread
1165
	 * @author azabala
1166
	 *
1167
	 */
1168
	public class AddResultLayerTask implements IMonitorableTask {
1169
		int initialStep = 1;
650
	
1170 651

  
1171
		int currentStep = 5;
1172

  
1173
		int lastStep = 10;
1174

  
1175
		boolean finished = false;
1176

  
1177
		IGeoprocess geoprocess;
1178

  
1179
		public AddResultLayerTask(IGeoprocess geoprocess) {
1180
			this.geoprocess = geoprocess;
1181
		}
1182

  
1183
		public int getInitialStep() {
1184
			return initialStep;
1185
		}
1186

  
1187
		public int getFinishStep() {
1188
			return lastStep;
1189
		}
1190

  
1191
		public int getCurrentStep() {
1192
			if (!finished)
1193
				return 5;
1194
			else
1195
				return lastStep;
1196
		}
1197

  
1198
		public String getStatusMessage() {
1199
			return "Loading layer...";
1200
		}
1201

  
1202
		public String getNote() {
1203
			return "";
1204
		}
1205

  
1206
		public boolean isDefined() {
1207
			return true;
1208
		}
1209

  
1210
		public void cancel() {
1211
			finished = true;
1212
		}
1213

  
1214
		public void run() throws GeoprocessException {
1215
			try {
1216
				layers.addLayer(geoprocess.getResult());
1217
			} catch (CancelationException e) {
1218
				throw new GeoprocessException(
1219
						"Error al a?adir el resultado de un geoproceso a flayers");
1220
			} finally {
1221
				finished = true;
1222
			}
1223
		}
1224

  
1225
		public boolean isCanceled() {
1226
			return false;
1227
		}
1228

  
1229
		public boolean isFinished() {
1230
			return finished;
1231
		}
1232
	}
1233

  
1234 652
	/**
1235 653
	 * This method initializes mainPanel	
1236 654
	 * 	

Also available in: Unified diff