Revision 1902

View differences:

org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
  <modelVersion>4.0.0</modelVersion>
4
  <artifactId>org.gvsig.geoprocess.algorithm.fusespatially</artifactId>
5
  <packaging>jar</packaging>
6
  <name>org.gvsig.geoprocess.algorithm.fusespatially</name>
7
	
8
	<parent>
9
		<groupId>org.gvsig</groupId>
10
		<artifactId>org.gvsig.geoprocess.algorithm</artifactId>
11
		<version>2.2.248</version>
12
	</parent>
13
	
14
	<dependencies>
15
		<dependency>
16
		    <groupId>org.gvsig</groupId>
17
   			<artifactId>org.gvsig.geoprocess.algorithm.base</artifactId>
18
            <scope>compile</scope>
19
   		</dependency>
20
   		<dependency>
21
		    <groupId>org.gvsig</groupId>
22
   			<artifactId>org.gvsig.geoprocess.algorithm.dissolve</artifactId>
23
            <scope>compile</scope>
24
   		</dependency>
25
	</dependencies>
26
	
27
</project>
0 28

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/resources/org/gvsig/geoprocess/algorithm/fusespatially/fusespatially.properties
1
#
2
# gvSIG. Desktop Geographic Information System.
3
#
4
# Copyright (C) 2007-2012 gvSIG Association.
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
# MA  02110-1301, USA.
20
#
21
# For any additional information, do not hesitate to contact us
22
# at info AT gvsig.com, or visit our website www.gvsig.com.
23
#
24

  
25
basic_vect_algorithms=Capas vectoriales
26
Input_layer=Capa de entrada
27
Selected_geometries_fuse=Geom. seleccionadas (Capa entrada)
28
Field=Campo
29
Function_list=Lista de funciones
30
fusespatially=Fusionar espacialmente
31
fuse_spatially=Fusionar espacialmente
32
summary_function=Funci?n resumen
33
adjacent_geometries_only=Solo geometrias adyacentes
0 34

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/resources/org/gvsig/geoprocess/algorithm/fusespatially/fusespatially_en.properties
1
#
2
# gvSIG. Desktop Geographic Information System.
3
#
4
# Copyright (C) 2007-2012 gvSIG Association.
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
# MA  02110-1301, USA.
20
#
21
# For any additional information, do not hesitate to contact us
22
# at info AT gvsig.com, or visit our website www.gvsig.com.
23
#
24

  
25
basic_vect_algorithms=Vector layers tools
26
Input_layer=Input cover
27
Selected_geometries_fuse=Selected geometries (Input cover)
28
Field=Field
29
Function_list=Function list
30
fusespatially=Fuse spatially
31
fuse_spatially=Fuse spatially
32
summary_function=Summary function
33
adjacent_geometries_only=Only dissolve adjacent
34
groupby=Group by a field
0 35

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/resources/help/FuseSpatiallyAlgorithm.xml
1
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes' ?>
2
<!--
3

  
4
    gvSIG. Desktop Geographic Information System.
5

  
6
    Copyright (C) 2007-2012 gvSIG Association.
7

  
8
    This program is free software; you can redistribute it and/or
9
    modify it under the terms of the GNU General Public License
10
    as published by the Free Software Foundation; either version 2
11
    of the License, or (at your option) any later version.
12

  
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17

  
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
    MA  02110-1301, USA.
22

  
23
    For any additional information, do not hesitate to contact us
24
    at info AT gvsig.com, or visit our website www.gvsig.com.
25

  
26
-->
27
	<help>
28
		<element name="DESCRIPTION" text="Este geoproceso act&#250;a sobre una sola capa de entrada, cuyo tipo de geometr&#237;a ha de ser forzosamente de pol&#237;gonos. El proceso analiza cada pol&#237;gono de la capa de entrada, de tal forma que fusionar&#225; en un solo pol&#237;gono aquellos pol&#237;gonos que coincidan espacialmente, es decir que intersequen entre ellos. La tabla de atributos de los pol?gonos resultantes estar? formada por un campo ID que ser? el identificador del fen?meno. <BR> Adicionalmente se crear? otra capa que contendr? ?nicamente informaci?n alfanum?rica con la tabla de datos de la capa de origen. Adem?s incluir? un campo ID en el que tendr? la referencia al ID de la capa de pol?gonos creada. De esta forma, cada pol?gono har? referencia con su ID a una o varias entradas de la capa unicamente alfanum?rica para identificar la informaci?n asociada originalmente a cada pol?gono de la capa." description="Descripci&#243;n" type="0">
29
			<image description="" file="fusespatiallydesc.png">
30
			</image>
31
		</element>
32
		<element name="ADDITIONAL_INFO" text="" description="Informaci&#243;n adicional" type="0">
33
		</element>
34
		<element name="EXTENSION_AUTHOR" text="Nacho Brodin" description="Algoritmo creado por" type="0">
35
		</element>
36
		<element name="HELP_AUTHOR" text="" description="Ayuda creada por" type="0">
37
		</element>
38
		<element name="USER_NOTES" text="" description="Notas de usuario" type="0">
39
		</element>
40
		<element name="LAYER" text="" description="Capa de entrada" type="3">
41
		</element>
42
		<element name="SELECTED_GEOM" text="" description="Geometrias seleccionadas" type="3">
43
		</element>
44
		<element name="DISSOLV_ADJ" text="" description="Geometrias seleccionadas" type="3">
45
		</element>
46
		<element name="FIELD" text="" description="Campo" type="3">
47
		</element>
48
		<element name="FUNCTION_LIST" text="" description="Lista de funciones" type="3">
49
		</element>
50
		<element name="OUTPUT_DESCRIPTION" text="" description="Descripci&#243;n" type="2">
51
		</element>
52
		<element name="RESULT" text="" description="Disolver" type="2">
53
		</element>
54
	</help>
55
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes' ?>
0 56

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/resources/help/FuseSpatiallyAlgorithm_en.xml
1
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes' ?>
2
<!--
3

  
4
    gvSIG. Desktop Geographic Information System.
5

  
6
    Copyright (C) 2007-2012 gvSIG Association.
7

  
8
    This program is free software; you can redistribute it and/or
9
    modify it under the terms of the GNU General Public License
10
    as published by the Free Software Foundation; either version 2
11
    of the License, or (at your option) any later version.
12

  
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17

  
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
    MA  02110-1301, USA.
22

  
23
    For any additional information, do not hesitate to contact us
24
    at info AT gvsig.com, or visit our website www.gvsig.com.
25

  
26
-->
27
	<help>
28
		<element name="DESCRIPTION" text="This geoprocess works with one only input layer, whose geometry type must be polygon. This process analize each input geometry and builds a unique polygon with all geometries which intersects each other. The attribute table will have an identifier field of each feature. <BR> Furthermore, other layer with only alphanumeric data will be created. This alphanumeric table will contain the table of the input layer and it will include a identifier field with a reference to the polygon in the first layer. In this way, each polygon in the first layer will have a reference to each feature in the second layer." description="Descripci&#243;n" type="0">
29
			<image description="" file="fusespatiallydesc.png">
30
			</image>
31
		</element>
32
		<element name="ADDITIONAL_INFO" text="" description="Informaci&#243;n adicional" type="0">
33
		</element>
34
		<element name="EXTENSION_AUTHOR" text="Nacho Brodin" description="Algoritmo creado por" type="0">
35
		</element>
36
		<element name="HELP_AUTHOR" text="" description="Ayuda creada por" type="0">
37
		</element>
38
		<element name="USER_NOTES" text="" description="Notas de usuario" type="0">
39
		</element>
40
		<element name="LAYER" text="" description="Capa de entrada" type="3">
41
		</element>
42
		<element name="SELECTED_GEOM" text="" description="Geometrias seleccionadas" type="3">
43
		</element>
44
		<element name="DISSOLV_ADJ" text="" description="Geometrias seleccionadas" type="3">
45
		</element>
46
		<element name="FIELD" text="" description="Campo" type="3">
47
		</element>
48
		<element name="FUNCTION_LIST" text="" description="Lista de funciones" type="3">
49
		</element>
50
		<element name="OUTPUT_DESCRIPTION" text="" description="Descripci&#243;n" type="2">
51
		</element>
52
		<element name="RESULT" text="" description="Disolver" type="2">
53
		</element>
54
	</help>
55
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes' ?>
0 56

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.geoprocess.algorithm.fusespatially.FuseSpatiallyLibrary
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/java/org/gvsig/geoprocess/algorithm/fusespatially/FuseSpatiallyLibrary.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.fusespatially;
25

  
26
import org.gvsig.geoprocess.algorithm.base.core.AlgorithmAbstractLibrary;
27
import org.gvsig.i18n.Messages;
28
import org.gvsig.tools.library.LibraryException;
29

  
30
/**
31
 * Initialization of FuseSpatiallyLibrary library.
32
 * 
33
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
34
 */
35
public class FuseSpatiallyLibrary extends AlgorithmAbstractLibrary {
36

  
37
    @Override
38
    protected void doInitialize() throws LibraryException {
39

  
40
    }
41

  
42
    @Override
43
    protected void doPostInitialize() throws LibraryException {
44
        Messages.addResourceFamily(
45
            "org.gvsig.geoprocess.algorithm.fusespatially.fusespatially",
46
            FuseSpatiallyLibrary.class.getClassLoader(), FuseSpatiallyLibrary.class
47
                .getClass().getName());
48

  
49
        registerGeoProcess(new FuseSpatiallyAlgorithm());
50
    }
51

  
52
}
0 53

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/java/org/gvsig/geoprocess/algorithm/fusespatially/FuseSpatiallyAlgorithm.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.fusespatially;
25

  
26
import java.util.ArrayList;
27
import java.util.List;
28

  
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
32
import org.gvsig.fmap.dal.feature.FeatureSet;
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.geoprocess.algorithm.dissolve.DissolveAlgorithm;
35
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
36
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
37
import org.gvsig.tools.task.SimpleTaskStatus;
38

  
39
import es.unex.sextante.core.Sextante;
40
import es.unex.sextante.dataObjects.IVectorLayer;
41
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
42
import es.unex.sextante.exceptions.RepeatedParameterNameException;
43
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
44
import es.unex.sextante.outputs.NullOutputChannel;
45
import es.unex.sextante.outputs.OutputVectorLayer;
46
import es.unex.sextante.parameters.Parameter;
47
import java.util.Iterator;
48

  
49
/**
50
 * Fuse spatially algorithm
51
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52
 */
53
public class FuseSpatiallyAlgorithm extends AbstractSextanteGeoProcess {
54

  
55
	public static final String         RESULT            = "RESULT";
56
	public static final String         RESULT_TABLE      = "RESULT_TABLE";
57
	public static final String         LAYER             = "LAYER";
58
	public static final String         SELECTED_GEOM     = "SELECTED_GEOM";
59
	private AbstractSextanteGeoProcess process           = null;
60
	private String                     fid               = "FID";
61

  
62
	/*
63
	 * (non-Javadoc)
64
	 * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
65
	 */
66
	public void defineCharacteristics(){
67
        setName(getTranslation("fusespatially"));
68
        setGroup(getTranslation("basic_vect_algorithms"));
69
        // setGeneratesUserDefinedRasterOutput(false);
70
		try {
71
			m_Parameters.addInputVectorLayer(LAYER, getTranslation("Input_layer"), IVectorLayer.SHAPE_TYPE_WRONG, true);
72
			m_Parameters.addBoolean(SELECTED_GEOM, getTranslation("Selected_geometries_fuse"), false);
73
			addOutputVectorLayer(RESULT, getTranslation("fuse_spatially") + " ", OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
74
			addOutputVectorLayer(RESULT_TABLE, getTranslation("fuse_spatially") + "_" + getTranslation("Table") + " ", OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
75
		} catch (RepeatedParameterNameException e) {
76
			Sextante.addErrorToLog(e);
77
		}
78
	}
79

  
80
	public void setParentProcess(AbstractSextanteGeoProcess process) {
81
		this.process = process;
82
	}
83

  
84
	/*
85
	 * (non-Javadoc)
86
	 * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
87
	 */
88
	public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
89
		if(existsOutPutFile(DissolveAlgorithm.RESULT, 0)) {
90
    		throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
91
    	}
92
		IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
93
		boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
94

  
95
		FeatureStore storeLayer = null;
96
		if(layer instanceof FlyrVectIVectorLayer)
97
			storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
98
		else
99
			return false;
100

  
101
		try {
102

  
103

  
104
                    boolean createTable;
105
                    FeatureStore outFeatStore;
106
                    if (this.getOutputChannel("RESULT_TABLE") instanceof NullOutputChannel) {
107
                        createTable = false;
108
                        String[] attrNames = new String[storeLayer.getDefaultFeatureType().getAttributeDescriptors().length+1];
109
                        Class[] types = new Class[storeLayer.getDefaultFeatureType().getAttributeDescriptors().length+1];
110
                        attrNames[0] = "FID";
111
                        types[0] = Integer.class;
112
                        int count = 1;
113
                        for (FeatureAttributeDescriptor attributeDescriptor : storeLayer.getDefaultFeatureType().getAttributeDescriptors()) {
114
                            attrNames[count] = attributeDescriptor.getName();
115
                            types[count] = attributeDescriptor.getObjectClass();
116
                            count = count + 1;
117
                        }
118
                        
119
                        outFeatStore = buildFuseSpatiallyOutPutStore(attrNames, types,
120
                                layer.getShapeType(), getTranslation("fusespatially"), RESULT);
121
                    } else {
122
                        String[] attrNames = new String[]{"FID"};
123
                        Class[] types = new Class[]{Integer.class};
124
                        createTable = true;
125
                        outFeatStore = buildFuseSpatiallyOutPutStore(attrNames, types,
126
                                layer.getShapeType(), getTranslation("fusespatially"), RESULT);
127
                    }
128

  
129
			return execute(storeLayer, outFeatStore, layer.getShapeType(), selectedGeom, getStatus(), "FID", createTable);
130
		} catch (DataException e) {
131
			Sextante.addErrorToLog(e);
132
			return false;
133
		}
134
	}
135

  
136
	/**
137
	 * @param inputStoreLayer
138
	 * @param outFeatStore
139
	 * @param shapeType
140
	 * @param selectedGeom
141
	 * @param status
142
	 * @param idField
143
	 * @param createTable
144
	 * @return boolean
145
	 * @throws DataException
146
	 */
147
	public boolean execute(FeatureStore inputStoreLayer,
148
			FeatureStore outFeatStore,
149
			int shapeType,
150
			boolean selectedGeom,
151
			SimpleTaskStatus status,
152
			String idField,
153
			boolean createTable) throws DataException {
154
		FeatureStore outFeatStoreTable = null;
155
		String[] attrNamesTable = null;
156
		if(createTable) {
157
			attrNamesTable = new String[inputStoreLayer.getDefaultFeatureType().size() + 1];
158
			Class[] typesTable = new Class[inputStoreLayer.getDefaultFeatureType().size() + 1];
159
			attrNamesTable[0] = fid;
160
			typesTable[0] = Integer.class;
161
			for (int i = 0; i < inputStoreLayer.getDefaultFeatureType().size(); i++) {
162
				FeatureAttributeDescriptor attrDesc = inputStoreLayer.getDefaultFeatureType().getAttributeDescriptor(i);
163
				attrNamesTable[i + 1] = attrDesc.getName();
164
				typesTable[i + 1] = attrDesc.getDataType().getDefaultClass();
165
			}
166

  
167
			attrNamesTable = checkFields(attrNamesTable);
168
			outFeatStoreTable = buildFuseSpatiallyOutPutStore(attrNamesTable, typesTable,
169
				shapeType, getTranslation("fusespatially") + "_Table", RESULT_TABLE);
170
		}
171

  
172
		FuseSpatiallyOperationFast2 operation = new FuseSpatiallyOperationFast2(this);
173
		operation.setTaskStatus(getStatus());
174
		operation.computesGeometryOperation(inputStoreLayer,
175
				outFeatStore,
176
				outFeatStoreTable,
177
				new String[]{fid},
178
				attrNamesTable,
179
				selectedGeom,
180
				false,
181
				idField);
182

  
183
		if(getTaskMonitor().isCanceled())
184
			return false;
185
		/*computesGeometryOperation(inputStoreLayer, outFeatStore, outFeatStoreTable,
186
				attrNames, attrNamesTable, selectedGeom, status, idField);*/
187
		return true;
188
	}
189

  
190
	/**
191
	 * Removes duplicate fields
192
	 * @param names
193
	 * @return
194
	 */
195
	public String[] checkFields(String[] names) {
196
		if(names.length <= 1)
197
			return names;
198
		int cont = 0;
199

  
200
		int i = 1;
201
		while(i < names.length) {
202
			if(names[0].compareTo(names[i]) == 0) {
203
				names[0] = "FID_" + cont;
204
				i = 0;
205
				cont ++;
206
			}
207
			i ++;
208
		}
209
		return names;
210
	}
211

  
212

  
213
	/**
214
	 * Computes a complete operation over the input FeatureStore. The result of this operation
215
	 * is stored in the output FeatureStore. This method will call once for each geometry.
216
	 * @param inFeatStore
217
	 *        Input FeatureStore
218
	 * @param outFeatStore
219
	 *        Output FeatureStore
220
	 * @param attrNames
221
	 *        List of attributes to build the output feature store
222
	 * @param selectedGeom
223
	 *        If it is true only the selected geometries will be processed
224
	 * @deprecated This method uses FuseSpatiallyOperation which is deprecated
225
	 * @throws DataException
226
	 */
227
	@SuppressWarnings("deprecation")
228
	public void computesGeometryOperation(FeatureStore inFeatStore,
229
									FeatureStore outFeatStore,
230
									FeatureStore outFeatStoreTable,
231
									String[] attrNames,
232
									String[] attrNamesTable,
233
									boolean selectedGeom,
234
									SimpleTaskStatus status,
235
									String idField) throws DataException {
236
		FeatureSet featuresSet = null;
237
		Iterator it = null;
238

  
239
		if(selectedGeom) {
240
			featuresSet = (FeatureSet)inFeatStore.getSelection();
241
		} else {
242
			featuresSet = inFeatStore.getFeatureSet();
243
		}
244

  
245
		it = featuresSet.iterator();
246
		int numberOfFeatures = (int)featuresSet.getSize();
247
        if (status != null) {
248
            status.setRangeOfValues(0, numberOfFeatures);
249
        }
250

  
251
        //Stack<Feature> featList = new Stack<Feature>();
252
        List<Feature> featList = new ArrayList<Feature>();
253
		while( it.hasNext() ) {
254
			Feature feature = (Feature)it.next();
255
			featList.add(feature);
256
		}
257

  
258

  
259
		FuseSpatiallyOperation operation = new FuseSpatiallyOperation(featList,
260
				outFeatStoreTable,
261
				attrNamesTable,
262
				idField,
263
				this);
264
		operation.setFeatureStore(outFeatStore, attrNames);
265
		int size = featList.size();
266
		operation.setGeoProcess(this, size);
267

  
268
		while (featList.size() > 0 && !m_Task.isCanceled()) {
269
			Feature f = featList.remove(featList.size() - 1);
270
			operation.invoke(f.getDefaultGeometry(), f);
271
			setProgress(size - featList.size(), size);
272
		}
273

  
274
		if(operation.getWriter() != null)
275
			operation.getWriter().end();
276
	}
277

  
278
	public boolean setProgress(int step, int size) {
279
		if(process != null)
280
			return process.setProgress(step, size);
281
		else
282
			return super.setProgress(step, size);
283
	}
284

  
285
	/**
286
	 * Builds the output FeatureStore
287
	 * @param featureType
288
	 * @return FeatureStore
289
	 */
290
	@SuppressWarnings("unchecked")
291
	protected FeatureStore buildFuseSpatiallyOutPutStore(String[] attrNames,
292
											Class[] types,
293
											int shapeType,
294
											String sextanteLayerName,
295
											String sextanteLayerLabel) {
296

  
297

  
298
		try {
299
			IVectorLayer output = getNewVectorLayer(sextanteLayerLabel,
300
													sextanteLayerName,
301
													shapeType, types, attrNames);
302
			return ((FlyrVectIVectorLayer)output).getFeatureStore();
303
		} catch (UnsupportedOutputChannelException e) {
304
			Sextante.addErrorToLog(e);
305
        } catch (GeoAlgorithmExecutionException e) {
306
            Sextante.addErrorToLog(e);
307
        }
308
		return null;
309
	}
310

  
311
}
0 312

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/java/org/gvsig/geoprocess/algorithm/fusespatially/FuseSpatiallyOperationFast.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25

  
26
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2010 Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 */
44

  
45
package org.gvsig.geoprocess.algorithm.fusespatially;
46

  
47
import java.util.ArrayList;
48
import java.util.List;
49

  
50
import org.gvsig.fmap.dal.exception.DataException;
51
import org.gvsig.fmap.dal.feature.EditableFeature;
52
import org.gvsig.fmap.dal.feature.Feature;
53
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
54
import org.gvsig.fmap.dal.feature.FeatureSelection;
55
import org.gvsig.fmap.dal.feature.FeatureSet;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.geom.exception.CreateGeometryException;
58
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
59
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
60
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

  
64
import com.vividsolutions.jts.geom.Geometry;
65
import java.util.Iterator;
66
/**
67
 * Fuse spatially operation
68
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
69
 */
70
public class FuseSpatiallyOperationFast extends GeometryOperation {
71
	private static Logger 			         logger    		    = LoggerFactory.getLogger(FuseSpatiallyOperationFast.class.getName());
72
	private ArrayList<Element>               featureList        = null; 
73
	private String                           nameIdField        = null;
74
	private FeatureStore                     outFeatStoreTable  = null;
75
	
76
	class Element {
77
		public int                 id              = -1;
78
		public Feature             feat            = null;
79
		public List<Element>       overlapList     = new ArrayList<Element>();
80
		public boolean             insertedToJoin  = false;
81
		public Geometry            jtsGeom         = null;
82
	}
83
	
84
	public FuseSpatiallyOperationFast(AbstractSextanteGeoProcess process) {
85
		super(process);
86
		featureList = new ArrayList<Element>();
87
	}
88

  
89
	@Override
90
	public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g,
91
			Feature featureInput) {
92
		// TODO Auto-generated method stub
93
		return null;
94
	}
95

  
96
	@Override
97
	public void invoke(org.gvsig.fmap.geom.Geometry g,
98
			EditableFeature featureInput) {
99
		// TODO Auto-generated method stub
100
		
101
	}
102
	
103
	/**
104
	 * Computes a complete operation over the input FeatureStore. The result of this operation
105
	 * is stored in the output FeatureStore. This method will call once for each geometry.
106
	 * @param inFeatStore
107
	 *        Input FeatureStore
108
	 * @param outFeatStore
109
	 *        Output FeatureStore
110
	 * @param attrNames
111
	 *        List of attributes to build the output feature store
112
	 * @param selectedGeom
113
	 *        If it is true only the selected geometries will be processed
114
	 * @throws DataException
115
	 */
116
	@SuppressWarnings("deprecation")
117
	public void computesGeometryOperation(FeatureStore inFeatStore,
118
									FeatureStore outFeatStore,
119
									FeatureStore outFeatStoreTable,
120
									String[] attrNames,
121
									String[] attrNamesTable,
122
									boolean selectedGeomInput,
123
									boolean selectedGeomOutput,
124
									String idField) throws DataException {
125
		this.outFeatStoreTable = outFeatStoreTable;
126
		this.nameIdField = idField;
127
		this.inFeatureStore = inFeatStore;
128
		this.selectedGeomInput = selectedGeomInput;
129
		this.selectedGeomOverlay = selectedGeomOutput;
130
		FeatureSet featuresSet = null;
131
		featuresSet = inFeatStore.getFeatureSet();
132
		
133
		setFeatureStore(outFeatStore, attrNames);
134
		Iterator it = null;
135

  
136
		if(selectedGeomInput) {
137
            FeatureSelection ds = inFeatStore.getFeatureSelection();
138
            it = ds.iterator();
139
            numberOfFeatures = (int) ds.getSelectedCount();
140
		} else {
141
			it = featuresSet.iterator();
142
			numberOfFeatures = (int)featuresSet.getSize();
143
		}
144
		
145
        if (status != null) 
146
            status.setRangeOfValues(0, numberOfFeatures);
147
        if(process != null) 
148
            process.setProgress(0, numberOfFeatures);
149
		
150
        //Crear lista de elementos
151
		int iCount = 0;
152
		while( it.hasNext() && !process.getTaskMonitor().isCanceled()) {
153
			Feature feat = (Feature)it.next();
154
			Element el = new Element();
155
			el.feat = feat;
156
			el.id = iCount;
157
			featureList.add(el);
158
            if (status != null && process != null) 
159
                status.setCurValue(iCount);
160
            if(process != null) 
161
                process.setProgress(iCount, numberOfFeatures);
162
			iCount ++;
163
		}
164
//		it.dispose();
165
		
166
		//Crear listas de solapes para cada feature
167
		iCount = 0;
168
		while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
169
			Element elem1 = featureList.get(iCount);
170
			org.gvsig.fmap.geom.Geometry geom1 = elem1.feat.getDefaultGeometry();
171
			if (status != null)  
172
                status.setCurValue(iCount);
173
            if(process != null)
174
                process.setProgress(iCount, numberOfFeatures);
175
            
176
			for (int i = iCount + 1; i < featureList.size(); i++) {
177
				Element elem2 = featureList.get(i);
178
				org.gvsig.fmap.geom.Geometry geom2 = elem2.feat.getDefaultGeometry();
179
				if(areBBoxesOverlaping(geom1, geom2)) {
180
					if(elem1.jtsGeom == null)
181
						elem1.jtsGeom = GeometryUtil.geomToJTS(geom1);
182
					elem2.jtsGeom = GeometryUtil.geomToJTS(geom2);
183
					if(elem1.jtsGeom.intersects(elem2.jtsGeom))	{
184
						elem1.overlapList.add(elem2);
185
						elem2.overlapList.add(elem1);
186
					}
187
				}
188
			}
189
			iCount ++;
190
		}
191
		
192
		//Se calculan las listas de geometrias a unir
193
		//Para cada feature se obtiene su lista de elementos que solapan y para 
194
		//cada elemento que solapa se obtiene su lista. Finalmente todas se unen y 
195
		//y se hace un insert de una feature nueva
196
		List<Geometry> listResult = new ArrayList<Geometry>();
197
		iCount = 0;
198
		int iFeat = 0;
199
		while (iCount < featureList.size() && !process.getTaskMonitor().isCanceled()) {
200
			Element elem1 = featureList.get(iCount);
201
			if (status != null) 
202
                status.setCurValue(iCount);
203
            if(process != null) 
204
                process.setProgress(iCount, numberOfFeatures);
205
      
206
			if(!elem1.insertedToJoin) {
207
				elem1.insertedToJoin = true;
208
				listResult.clear();
209
				try {
210
					insertInTable(outFeatStoreTable, elem1.feat, iFeat);
211
				} catch (DataException e) {
212
					logger.info("Imposible insertar en la tabla", e);
213
				}
214
				listResult.add(elem1.jtsGeom);
215
				
216
				buildListToJoin(listResult, elem1.overlapList, iFeat);
217
				
218
				Geometry newGeom = computesUnion(listResult, elem1);
219
				
220
				try {
221
					addFeatureToOutput(newGeom, elem1.feat, iFeat);
222
				} catch (CreateGeometryException e) {
223
					logger.info("Error a?adiendo geometr?a", e);
224
				} catch (DataException e) {
225
					logger.info("Error a?adiendo geometr?a", e);
226
				}
227
				iFeat ++;
228
			}
229
			iCount ++;
230
		}
231

  
232
		if(persister != null)
233
			persister.end();
234
		
235
	}
236
	
237
	private boolean areBBoxesOverlaping(org.gvsig.fmap.geom.Geometry g1, org.gvsig.fmap.geom.Geometry g2) {
238
		if(g1.getEnvelope().getMaximum(0) < g2.getEnvelope().getMinimum(0))
239
			return false;
240
		if(g1.getEnvelope().getMinimum(0) > g2.getEnvelope().getMaximum(0))
241
			return false;
242
		if(g1.getEnvelope().getMaximum(1) < g2.getEnvelope().getMinimum(1))
243
			return false;
244
		if(g1.getEnvelope().getMinimum(1) > g2.getEnvelope().getMaximum(1))
245
			return false;
246
		return true;
247
	}
248
	
249
	/**
250
	 * Computes the union of the list of geometries
251
	 * @param listResult
252
	 * @param elem1
253
	 * @return
254
	 */
255
	private Geometry computesUnion(List<Geometry> listResult, Element elem1) {
256
		int splitValue = 500;
257
		Geometry newGeom = null;
258
		if(listResult.size() > splitValue) {
259
			List<List<Geometry>> list = splitList(listResult, splitValue);
260
			List<Geometry> result = new ArrayList<Geometry>();
261
			for (int i = 0; i < list.size(); i++) {
262
				Geometry aux = GeometryUtil.geometryUnion(list.get(i), elem1.feat.getDefaultGeometry().getGeometryType().getType());
263
				result.add(aux);
264
			}
265
			for (int i = 0; i < result.size(); i++) {
266
				newGeom = GeometryUtil.geometryUnion(result, elem1.feat.getDefaultGeometry().getGeometryType().getType());
267
			}
268
		} else {
269
			newGeom = GeometryUtil.geometryUnion(listResult, elem1.feat.getDefaultGeometry().getGeometryType().getType());	
270
		}
271
		return newGeom;
272
	}
273
	
274
	/**
275
	 * Splits the array of geometries to compute its union because JTS cannot support
276
	 * a lot of geometries
277
	 * @param list
278
	 * @param n
279
	 * @return
280
	 */
281
	private List<List<Geometry>> splitList(List<Geometry> list, int n) {
282
		int elements = (int)(list.size() / n);
283
		List<List<Geometry>> l = new ArrayList<List<Geometry>>();
284
		for (int i = 0; i < elements; i++) {
285
			l.add(list.subList((i * n), (i * n) + n));
286
		}
287
		if(elements * n < list.size()) {
288
			l.add(list.subList((elements * n), list.size()));
289
		}
290
		return l;
291
	}
292
	
293
	/**
294
	 * Builds the union of all lists 
295
	 * @param listResult
296
	 * @param listToJoin
297
	 */
298
	private void buildListToJoin(List<Geometry> listResult, List<Element> listToJoin, int reference) {
299
		for (int i = 0; i < listToJoin.size(); i++) {
300
			Element elem = listToJoin.get(i);
301
			if(!elem.insertedToJoin) {
302
				elem.insertedToJoin = true;
303
				buildListToJoin(listResult, elem.overlapList, reference);
304
				try {
305
					insertInTable(outFeatStoreTable, elem.feat, reference);
306
				} catch (DataException e) {
307
					logger.info("Imposible insertar en la tabla", e);
308
				}
309
				listResult.add(elem.jtsGeom);
310
			}
311
		}
312
	}
313
	
314
	/**
315
	 * Adds a feature to the output
316
	 * @param newGeom
317
	 * @param feat
318
	 * @param newFeatID
319
	 * @throws DataException
320
	 * @throws CreateGeometryException
321
	 */
322
	private void addFeatureToOutput(Geometry newGeom, 
323
			Feature feat, 
324
			int newFeatID) throws DataException, CreateGeometryException {
325
		//Si hay una tabla aparte esta capa solo lleva el id como referencia a la tabla de campos
326
		if(outFeatStoreTable != null) { 
327
			lastEditFeature = persister.addFeature(newGeom, nameIdField, newFeatID);
328
		} else {
329
			//Si no hay tabla se ponen todos los campos de la tabla de entrada
330
			String[] fieldNames = persister.getFieldNamesWithoutGeom();
331
			ArrayList<String> fields = new ArrayList<String>();
332
			ArrayList<Object> values = new ArrayList<Object>();
333
			fields.add(nameIdField);
334
			values.add(newFeatID);
335
			for (int j = 0; j < fieldNames.length; j++) {
336
				Object obj = feat.get(fieldNames[j]);
337
				if(obj != null && fieldNames[j].compareTo(nameIdField) != 0) {
338
					fields.add(fieldNames[j]);
339
					values.add(obj);
340
				}
341
			}
342
			lastEditFeature = persister.addFeature(newGeom, fields, values);
343
		}
344
	}
345
	
346
	/**
347
	 * Insert in the output table a new feature with the fields of the input feature. Moreover 
348
	 * exists a field that is an identifier which is a reference to the fusion layer.
349
	 * @param outFeatureStoreTable
350
	 * @param f
351
	 * @throws DataException
352
	 */
353
	private void insertInTable(FeatureStore outFeatureStoreTable, Feature f, int reference) throws DataException {
354
		if(outFeatureStoreTable == null)
355
			return;
356
		EditableFeature edFeat = outFeatureStoreTable.createNewFeature();
357
		edFeat.set(nameIdField, reference);
358
		FeatureAttributeDescriptor[] attrList = f.getType().getAttributeDescriptors();
359
		for (int j = 0; j < attrList.length; j++) {
360
			if(attrList[j].getName().compareTo("GEOMETRY") != 0) {
361
				edFeat.set(attrList[j].getName(), f.get(attrList[j].getName()));
362
			}
363
		}
364
		outFeatureStoreTable.insert(edFeat);
365
	}
366
	
367
}
368

  
0 369

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/java/org/gvsig/geoprocess/algorithm/fusespatially/FuseSpatiallyOperation_Old.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25

  
26
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2010 Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 */
44

  
45
package org.gvsig.geoprocess.algorithm.fusespatially;
46

  
47
import java.util.ArrayList;
48
import java.util.Iterator;
49
import java.util.List;
50

  
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.feature.EditableFeature;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.geom.GeometryLocator;
57
import org.gvsig.fmap.geom.GeometryManager;
58
import org.gvsig.fmap.geom.exception.CreateGeometryException;
59
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
60
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
61
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
62

  
63
import com.vividsolutions.jts.geom.Geometry;
64
/**
65
 * Fuse spatially operation
66
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
67
 */
68
public class FuseSpatiallyOperation_Old extends GeometryOperation {
69
	private FeatureStore                     inFeatStore      = null;
70
	protected GeometryManager                geomManager      = GeometryLocator.getGeometryManager();
71
	private FeatureSet                       featureSet       = null;
72
	private int                              id               = 0;
73
	private long                             nFeatures        = 0;
74
	private boolean[]                        featProcessed    = null;
75

  
76
	public FuseSpatiallyOperation_Old(FeatureStore inFeatStore, boolean selectedGeom, AbstractSextanteGeoProcess p) throws DataException {
77
		super(p);
78
		this.inFeatStore = inFeatStore;
79
		
80
		if(selectedGeom) {
81
			featureSet = (FeatureSet)inFeatStore.getSelection();
82
		} else
83
			featureSet = inFeatStore.getFeatureSet();
84
		nFeatures = featureSet.getSize();
85
		featProcessed = new boolean[(int)nFeatures];
86
	}
87

  
88
	/*
89
	 * (non-Javadoc)
90
	 * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
91
	 */
92
	public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
93
		if(g == null)
94
			return lastEditFeature;
95
		
96
		try {
97
			Geometry inputJTSGeom = GeometryUtil.geomToJTS(g);
98
			Geometry union = null; 
99
			
100
			Iterator it = featureSet.fastIterator();
101
			int index = 0;
102
			while(it.hasNext()) {
103
				Feature f = (Feature)it.next();
104
				if(featProcessed[index])
105
					continue;
106
				org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
107
				Geometry jtsGeom = GeometryUtil.geomToJTS(geom); //Multigeometry support
108
				if(inputJTSGeom.intersects(jtsGeom)) {
109
					inputJTSGeom = inputJTSGeom.union(jtsGeom);
110
					featProcessed[index] = true;
111
				}
112
				index ++;
113
				continue;
114
			}
115

  
116
			lastEditFeature = persister.addFeature(inputJTSGeom, "FID", id);
117
		} catch (DataException e) {
118
			return null;
119
		} catch (CreateGeometryException e) {
120
			return null;
121
		}
122
		id++;
123
		return lastEditFeature;
124
	}
125
	
126
	/*
127
	 * (non-Javadoc)
128
	 * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
129
	 */
130
	public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
131
		if(g == null)
132
			return;
133
	}
134
	
135
	/*
136
	 * (non-Javadoc)
137
	 * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
138
	 */
139
	public Object getResult() {
140
		return lastEditFeature;
141
	}
142
	
143
}
144

  
0 145

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/java/org/gvsig/geoprocess/algorithm/fusespatially/FuseSpatiallyOperation.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25

  
26
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2010 Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 */
44

  
45
package org.gvsig.geoprocess.algorithm.fusespatially;
46

  
47
import java.util.ArrayList;
48
import java.util.List;
49
import java.util.Stack;
50

  
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.feature.EditableFeature;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.geom.GeometryLocator;
57
import org.gvsig.fmap.geom.GeometryManager;
58
import org.gvsig.fmap.geom.exception.CreateGeometryException;
59
import org.gvsig.geoprocess.algorithm.base.core.DALFeaturePersister;
60
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
61
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
62
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
63

  
64
import com.vividsolutions.jts.geom.Geometry;
65
/**
66
 * Fuse spatially operation
67
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
68
 * @deprecated Uses FuseSpatiallyOperationFast
69
 */
70
public class FuseSpatiallyOperation extends GeometryOperation {
71
	protected GeometryManager                geomManager           = GeometryLocator.getGeometryManager();
72
	private int                              id                    = 0;
73
	private List<Feature>                    featList              = null;
74
	private FeatureStore                     outFeatureStoreTable  = null;
75
	private String                           idField               = "FID";
76
	
77
	public FuseSpatiallyOperation(List<Feature> f, 
78
			FeatureStore outFeatStoreTable, 
79
			String[] fieldNames, 
80
			String idField, 
81
			AbstractSextanteGeoProcess p) throws DataException {
82
		super(p);
83
		this.featList = f;
84
		this.outFeatureStoreTable = outFeatStoreTable;
85
		this.idField = idField;
86
	}
87
	
88
	public FuseSpatiallyOperation(Stack<Feature> f, 
89
			FeatureStore outFeatStoreTable, 
90
			String[] fieldNames, 
91
			AbstractSextanteGeoProcess p) throws DataException {
92
		super(p);
93
		this.featList = f;
94
		this.outFeatureStoreTable = outFeatStoreTable;
95
	}
96
	
97
	/*
98
	 * (non-Javadoc)
99
	 * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
100
	 */
101
        @Override
102
	public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
103
		try {
104
			Geometry jtsInputGeom = GeometryUtil.geomToJTS(g);
105
			insertInTable(feature);
106
			
107
			int cont = featList.size() - 1;
108
			while(cont >= 0 && !process.getTaskMonitor().isCanceled()) {
109
				Feature f = featList.get(cont);
110
				g = f.getDefaultGeometry();
111
				Geometry jtsGeom = GeometryUtil.geomToJTS(g); //Multigeometry support
112
				if(jtsGeom.intersects(jtsInputGeom)) {
113
					jtsInputGeom = jtsInputGeom.union(jtsGeom);
114
					featList.remove(cont);
115
					process.setProgress(procesSize - featList.size(), procesSize);
116
					cont = featList.size();
117
					insertInTable(f);
118
				}
119
				cont --;
120
			}
121

  
122
			//Cuando no genera dos capas mete los campos de la feature de entrada en la de salida perdiendose el resto
123
			if(outFeatureStoreTable == null) { 
124
				String[] fieldNames = persister.getFieldNamesWithoutGeom();
125
				ArrayList<String> fields = new ArrayList<String>();
126
				ArrayList<Object> values = new ArrayList<Object>();
127
				fields.add(idField);
128
				values.add(id);
129
				for (int j = 0; j < fieldNames.length; j++) {
130
					Object obj = feature.get(fieldNames[j]);
131
					if(obj != null && fieldNames[j].compareTo(idField) != 0) {
132
						fields.add(fieldNames[j]);
133
						values.add(obj);
134
					}
135
				}
136
				lastEditFeature = persister.addFeature(jtsInputGeom, fields, values);
137
			} else
138
				lastEditFeature = persister.addFeature(jtsInputGeom, idField, id);
139
		} catch (DataException e) {
140
			return null;
141
		} catch (CreateGeometryException e) {
142
			return null;
143
		}
144
		id++;
145
		return lastEditFeature;
146
	}
147
	
148
	private void insertInTable(Feature f) throws DataException {
149
		if(outFeatureStoreTable == null)
150
			return;
151
		EditableFeature edFeat = outFeatureStoreTable.createNewFeature();
152
		edFeat.set(idField, id);
153
		FeatureAttributeDescriptor[] attrList = f.getType().getAttributeDescriptors();
154
		for (int j = 0; j < attrList.length; j++) {
155
			if(attrList[j].getName().compareTo("GEOMETRY") != 0) {
156
				edFeat.set(attrList[j].getName(), f.get(attrList[j].getName()));
157
			}
158
		}
159
		outFeatureStoreTable.insert(edFeat);
160
	}
161
	
162
	/**
163
	 * Removes duplicate fields
164
	 * @param names
165
	 * @return
166
	 */
167
	public static String[] checkFields(String[] names) {
168
		if(names.length <= 1)
169
			return names;
170
		int cont = 0;
171

  
172
		int i = 1;
173
		while(i < names.length) {
174
			if(names[0].compareTo(names[i]) == 0) {
175
				names[0] = "FID_" + cont;
176
				i = 0;
177
				cont ++;
178
			}
179
			i ++;
180
		}
181
		return names;
182
	}
183
	
184
	/*
185
	 * (non-Javadoc)
186
	 * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
187
	 */
188
        @Override
189
	public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
190
		if(g == null)
191
			return;
192
	}
193
	
194
	/*
195
	 * (non-Javadoc)
196
	 * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
197
	 */
198
	public Object getResult() {
199
		return lastEditFeature;
200
	}
201
	
202
	public DALFeaturePersister getWriter() {
203
		return persister;
204
	}
205
	
206
}
207

  
0 208

  
org.gvsig.geoprocess/tags/org.gvsig.geoprocess-2.2.248/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.fusespatially/src/main/java/org/gvsig/geoprocess/algorithm/fusespatially/FuseSpatiallyOperationFast2.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25

  
26
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
27
 *
28
 * Copyright (C) 2010 Generalitat Valenciana.
29
 *
30
 * This program is free software; you can redistribute it and/or
31
 * modify it under the terms of the GNU General Public License
32
 * as published by the Free Software Foundation; either version 2
33
 * of the License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU General Public License
41
 * along with this program; if not, write to the Free Software
42
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
43
 */
44
package org.gvsig.geoprocess.algorithm.fusespatially;
45

  
46
import java.util.ArrayList;
47
import java.util.List;
48

  
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.feature.EditableFeature;
51
import org.gvsig.fmap.dal.feature.Feature;
52
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
53
import org.gvsig.fmap.dal.feature.FeatureSelection;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.geom.exception.CreateGeometryException;
57
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
58
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
59
import org.gvsig.geoprocess.algorithm.base.util.JTSFacade;
60
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

  
64
import com.vividsolutions.jts.geom.Geometry;
65
import java.util.Iterator;
66

  
67
/**
68
 * Fuse spatially operation
69
 *
70
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
71
 */
72
public class FuseSpatiallyOperationFast2 extends GeometryOperation {
73

  
74
    private static Logger logger = LoggerFactory.getLogger(FuseSpatiallyOperationFast2.class.getName());
75
    private ArrayList<Element> featureList = null;
76
    private String nameIdField = null;
77
    private FeatureStore outFeatStoreTable = null;
78
    private NodeTree baseTree = null;
79

  
80
    class Element {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff