svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src-test / org / gvsig / rastertools / vectorization / TestRasterPotrace.java @ 23468
History | View | Annotate | Download (7.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.rastertools.vectorization; |
20 |
|
21 |
import java.io.File; |
22 |
import java.sql.Types; |
23 |
import java.text.DecimalFormat; |
24 |
import java.text.NumberFormat; |
25 |
|
26 |
import org.cresques.cts.ProjectionPool; |
27 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
28 |
import org.gvsig.raster.dataset.io.RasterDriverException; |
29 |
import org.gvsig.raster.vectorization.VectorizationBinding; |
30 |
import org.gvsig.rastertools.vectorizacion.fmap.BezierPathX; |
31 |
|
32 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
33 |
import com.hardcode.gdbms.engine.values.Value; |
34 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
35 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
36 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
37 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
38 |
import com.iver.cit.gvsig.fmap.core.FShape; |
39 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
40 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
41 |
import com.iver.cit.gvsig.fmap.drivers.DXFLayerDefinition; |
42 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
43 |
import com.iver.cit.gvsig.fmap.drivers.LayerDefinition; |
44 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
45 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
46 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
47 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
48 |
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfFieldsMapping; |
49 |
import com.iver.cit.gvsig.fmap.edition.writers.dxf.DxfWriter; |
50 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
51 |
/**
|
52 |
*
|
53 |
* @version 05/08/2008
|
54 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
55 |
*/
|
56 |
public class TestRasterPotrace { |
57 |
private String baseDir = "./test-images/"; |
58 |
private String fileName = "wheel"; |
59 |
// private String fileName = "letters";
|
60 |
// private String fileName = "potrace";
|
61 |
|
62 |
private String fileIn = baseDir + fileName + ".bmp"; |
63 |
// private String fileIn = baseDir + "NewLayer_6.tif";
|
64 |
|
65 |
private String fileOutBase = "/tmp/" + fileName; |
66 |
|
67 |
private Value values[]; |
68 |
private IWriter writer;
|
69 |
private int m_iGeometry = 0; |
70 |
|
71 |
public void generatePotrace(String fileIn, String fileOutBase, int trozos) { |
72 |
NumberFormat formatter = new DecimalFormat("000"); |
73 |
String s = formatter.format(trozos);
|
74 |
|
75 |
String fileOutShape = fileOutBase + s + ".shp"; |
76 |
String fieldName = fileName + s;
|
77 |
try {
|
78 |
VectorizationBinding binding = new VectorizationBinding(fileIn);
|
79 |
// binding.setCornerThreshold(-1);
|
80 |
double geometrias[] = binding.VectorizeBuffer(); |
81 |
|
82 |
String sFields[] = new String[2]; |
83 |
sFields[0] = "ID"; |
84 |
sFields[1] = fieldName + ""; |
85 |
|
86 |
LayerDefinition tableDef = null;
|
87 |
if(fileOutShape.endsWith(".dxf")) { |
88 |
writer = new DxfWriter();
|
89 |
((DxfWriter)writer).setFile(new File(fileOutShape)); |
90 |
ProjectionPool pool = new ProjectionPool();
|
91 |
((DxfWriter)writer).setProjection(pool.get("EPSG:23030"));
|
92 |
tableDef = new DXFLayerDefinition();
|
93 |
|
94 |
DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
|
95 |
((DxfWriter)writer).setFieldMapping(fieldsMapping); |
96 |
} |
97 |
if(fileOutShape.endsWith(".shp")) { |
98 |
writer = new ShpWriter();
|
99 |
((ShpWriter)writer).setFile(new File(fileOutShape)); |
100 |
tableDef = new SHPLayerDefinition();
|
101 |
} |
102 |
tableDef.setShapeType(FShape.LINE); |
103 |
|
104 |
int types[] = {Types.DOUBLE, Types.DOUBLE}; |
105 |
FieldDescription[] fields = new FieldDescription[sFields.length]; |
106 |
for (int i = 0; i < fields.length; i++) { |
107 |
fields[i] = new FieldDescription();
|
108 |
fields[i].setFieldName(sFields[i]); |
109 |
fields[i].setFieldType(types[i]); |
110 |
fields[i].setFieldLength(getDataTypeLength(types[i])); |
111 |
fields[i].setFieldDecimalCount(5);
|
112 |
} |
113 |
tableDef.setFieldsDesc(fields); |
114 |
tableDef.setName(fileOutShape); |
115 |
|
116 |
try {
|
117 |
writer.initialize(tableDef); |
118 |
writer.preProcess(); |
119 |
} catch (InitializeWriterException e) {
|
120 |
e.printStackTrace(); |
121 |
} catch (StartWriterVisitorException e) {
|
122 |
e.printStackTrace(); |
123 |
} |
124 |
|
125 |
values = new Value[2]; |
126 |
values[0] = ValueFactory.createValue(0); |
127 |
values[1] = ValueFactory.createValue(0); |
128 |
|
129 |
showPotrace(geometrias, trozos); |
130 |
|
131 |
writer.postProcess(); |
132 |
} catch (NotSupportedExtensionException e) {
|
133 |
e.printStackTrace(); |
134 |
} catch (RasterDriverException e) {
|
135 |
e.printStackTrace(); |
136 |
} catch (InterruptedException e) { |
137 |
e.printStackTrace(); |
138 |
} catch (Exception e) { |
139 |
e.printStackTrace(); |
140 |
} |
141 |
} |
142 |
|
143 |
public TestRasterPotrace() {
|
144 |
for (int i = 2; i <= 15; i++) { |
145 |
generatePotrace(fileIn, fileOutBase, i); |
146 |
} |
147 |
generatePotrace(fileIn, fileOutBase, 100);
|
148 |
} |
149 |
|
150 |
public void addShape(FShape shape, Value[] value) throws Exception { |
151 |
if (shape == null) |
152 |
return;
|
153 |
IGeometry geom = ShapeFactory.createGeometry(shape); |
154 |
addGeometry(geom, value); |
155 |
} |
156 |
|
157 |
public void addGeometry(IGeometry geom, Value[] value) throws Exception { |
158 |
DefaultFeature feat = new DefaultFeature(geom, value, Integer.toString(m_iGeometry)); |
159 |
IRowEdited editFeat = new DefaultRowEdited(feat, IRowEdited.STATUS_MODIFIED, m_iGeometry);
|
160 |
m_iGeometry++; |
161 |
writer.process(editFeat); |
162 |
} |
163 |
|
164 |
private void showPotrace(double[] potraceX, int trozos) throws Exception { |
165 |
BezierPathX pathX = new BezierPathX(trozos);
|
166 |
|
167 |
int cont = 1; |
168 |
while (true) { |
169 |
if ((cont >= potraceX.length) || (cont >= potraceX[0])) |
170 |
return;
|
171 |
switch ((int) potraceX[cont]) { |
172 |
case 0: // MoveTo |
173 |
pathX.moveTo(potraceX[cont + 1], potraceX[cont + 2]); |
174 |
cont += 3;
|
175 |
break;
|
176 |
case 1: // LineTo |
177 |
pathX.lineTo(potraceX[cont + 1], potraceX[cont + 2]); |
178 |
cont += 3;
|
179 |
break;
|
180 |
case 2: // CurveTo |
181 |
pathX.curveTo(potraceX[cont + 1], potraceX[cont + 2], potraceX[cont + 3], potraceX[cont + 4], potraceX[cont + 5], potraceX[cont + 6]); |
182 |
cont += 7;
|
183 |
break;
|
184 |
case 3: // closePath |
185 |
FPolyline2D line = new FPolyline2D(pathX);
|
186 |
addShape(line, values); |
187 |
pathX = new BezierPathX(trozos);
|
188 |
cont ++; |
189 |
break;
|
190 |
} |
191 |
} |
192 |
} |
193 |
|
194 |
/**
|
195 |
* Returns the length of field
|
196 |
* @param dataType
|
197 |
* @return length of field
|
198 |
*/
|
199 |
public int getDataTypeLength(int dataType) { |
200 |
switch (dataType) {
|
201 |
case Types.NUMERIC: |
202 |
case Types.DOUBLE: |
203 |
case Types.REAL: |
204 |
case Types.FLOAT: |
205 |
case Types.BIGINT: |
206 |
case Types.INTEGER: |
207 |
case Types.DECIMAL: |
208 |
return 20; |
209 |
case Types.CHAR: |
210 |
case Types.VARCHAR: |
211 |
case Types.LONGVARCHAR: |
212 |
return 254; |
213 |
case Types.DATE: |
214 |
return 8; |
215 |
case Types.BOOLEAN: |
216 |
case Types.BIT: |
217 |
return 1; |
218 |
} |
219 |
return 0; |
220 |
} |
221 |
|
222 |
public static void main(String[] args){ |
223 |
new TestRasterPotrace();
|
224 |
} |
225 |
} |