Revision 14149

View differences:

trunk/libraries/libTopology/src-test/org/gvsig/topology/util/LayerFactory.java
1
/*
2
 * Created on 24-sep-2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id$
47
* $Log$
48
*
49
*/
50
package org.gvsig.topology.util;
51

  
52
import java.util.ArrayList;
53

  
54
import com.hardcode.gdbms.engine.values.StringValue;
55
import com.hardcode.gdbms.engine.values.Value;
56
import com.hardcode.gdbms.engine.values.ValueFactory;
57
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
58
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
59
import com.iver.cit.gvsig.fmap.core.FShape;
60
import com.iver.cit.gvsig.fmap.core.IFeature;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
63
import com.iver.cit.gvsig.fmap.drivers.FeatureCollectionMemoryDriver;
64
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
65
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition;
66
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
67
import com.vividsolutions.jts.geom.Coordinate;
68
import com.vividsolutions.jts.geom.Geometry;
69
import com.vividsolutions.jts.geom.GeometryFactory;
70
import com.vividsolutions.jts.geom.MultiPoint;
71
import com.vividsolutions.jts.geom.PrecisionModel;
72
import com.vividsolutions.jts.io.ParseException;
73
import com.vividsolutions.jts.io.WKTReader;
74

  
75
public class LayerFactory {
76
	static PrecisionModel pm = new PrecisionModel(10000);
77
	static GeometryFactory factory = new GeometryFactory(pm);
78
	static WKTReader wktReader = new WKTReader(factory);
79
	
80
	
81
	public static FLyrVect getLineLayerWithCollapsedCoords() throws ParseException{
82
		Geometry ln1 = wktReader.read("LINESTRING(500 500, 510 500)");
83
		Geometry ln2 = wktReader.read("LINESTRING(700 100, 720 120, 800 600)");
84
		
85
		FieldDescription fieldDescription = new FieldDescription();
86
		fieldDescription.setFieldName("str1");
87
		fieldDescription.setFieldType(FieldDescription.stringToType("String"));
88
		fieldDescription.setFieldLength(10);
89
		FieldDescription[] fields = {fieldDescription};
90
		LayerDefinition definition = new LayerDefinition();
91
		definition.setFieldsDesc(fields);
92
		definition.setShapeType(FShape.LINE);
93
		
94
		//feature 1
95
		StringValue value = ValueFactory.createValue("s1");
96
		Value[] values = {value};
97
		IGeometry iln1 = FConverter.jts_to_igeometry(ln1);
98
		DefaultFeature f1 = new DefaultFeature(iln1, values, "id1");
99
		
100
		
101
		
102
		
103
		StringValue value2 = ValueFactory.createValue("s2");
104
		Value[] values2 = {value2};
105
		IGeometry iln2 = FConverter.jts_to_igeometry(ln2);
106
		DefaultFeature f2 = new DefaultFeature(iln2, values2, "id2");
107
		
108
		
109
		ArrayList<IFeature> features = new ArrayList<IFeature>();
110
		features.add(f1);
111
		features.add(f2);
112
		
113
		
114
		FeatureCollectionMemoryDriver driver = 
115
			new FeatureCollectionMemoryDriver("multipuntos", 
116
				                                   features,
117
				                                   definition);
118
		
119
		return  (FLyrVect) com.iver.cit.gvsig.fmap.layers.LayerFactory.createLayer("multipuntos",
120
																driver, 
121
																null);
122
	}
123
	
124
	public static FLyrVect getLyrWithRepeatedCoords() throws ParseException{	
125
		Geometry multiPoint = wktReader.read("MULTIPOINT (520 160, 540 180, 540 180, 540 180, 600 180, 580 140, 640 140, 640 140, 640 160)");
126
		Geometry multiPoint2 = wktReader.read("MULTIPOINT (520 160, 540 180, 600 180, 580 140, 640 140,  640 160)");
127
		FieldDescription fieldDescription = new FieldDescription();
128
		fieldDescription.setFieldName("str1");
129
		fieldDescription.setFieldType(FieldDescription.stringToType("String"));
130
		fieldDescription.setFieldLength(10);
131
		FieldDescription[] fields = {fieldDescription};
132
		LayerDefinition definition = new LayerDefinition();
133
		definition.setFieldsDesc(fields);
134
		definition.setShapeType(FShape.MULTIPOINT);
135
		
136
		Coordinate[] coords = ((MultiPoint)multiPoint).getCoordinates();
137
		double[] x = new double[coords.length];
138
		double[] y = new double[coords.length];
139
		for(int i = 0; i < coords.length; i++){
140
			x[i] = coords[i].x;
141
			y[i] = coords[i].y;
142
		}
143
		FMultiPoint2D m1 = new FMultiPoint2D(x, y);
144
		StringValue value = ValueFactory.createValue("s1");
145
		Value[] values = {value};
146
		DefaultFeature f1 = new DefaultFeature(m1, values, "id1");
147
		
148
		Coordinate[] coords2 = ((MultiPoint)multiPoint2).getCoordinates();
149
		double[] x2 = new double[coords2.length];
150
		double[] y2 = new double[coords2.length];
151
		for(int i = 0; i < coords2.length; i++){
152
			x2[i] = coords2[i].x;
153
			y2[i] = coords2[i].y;
154
		}
155
		FMultiPoint2D m2 = new FMultiPoint2D(x2, y2);
156
		StringValue value2 = ValueFactory.createValue("s2");
157
		Value[] values2 = {value2};
158
		DefaultFeature f2 = new DefaultFeature(m2, values2, "id2");
159
		
160
		ArrayList<IFeature> features = new ArrayList<IFeature>();
161
		features.add(f1);
162
		features.add(f2);
163
		
164
		
165
		FeatureCollectionMemoryDriver driver = 
166
			new FeatureCollectionMemoryDriver("multipuntos", 
167
				                                   features,
168
				                                   definition);
169
		
170
		return  (FLyrVect) com.iver.cit.gvsig.fmap.layers.LayerFactory.createLayer("multipuntos",
171
																driver, 
172
																null);
173
	}
174
}
175

  

Also available in: Unified diff