svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / placements / PolygonPlacementParallel.java @ 40665
History | View | Annotate | Download (5.75 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 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.labeling.placements; |
42 |
|
43 |
import java.awt.Rectangle; |
44 |
import java.awt.geom.PathIterator; |
45 |
import java.awt.geom.Point2D; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Vector; |
48 |
import java.util.logging.Level; |
49 |
import java.util.logging.Logger; |
50 |
|
51 |
import com.iver.cit.gvsig.fmap.ViewPort; |
52 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
53 |
import com.iver.cit.gvsig.fmap.core.FShape; |
54 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
55 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
56 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
57 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IPlacementConstraints; |
58 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass; |
59 |
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelLocationMetrics; |
60 |
import com.iver.utiles.swing.threads.Cancellable; |
61 |
import com.vividsolutions.jts.geom.Geometry; |
62 |
import com.vividsolutions.jts.geom.Point; |
63 |
|
64 |
|
65 |
public class PolygonPlacementParallel implements ILabelPlacement{ |
66 |
|
67 |
|
68 |
public ArrayList<LabelLocationMetrics> guess(LabelClass lc, IGeometry geom, |
69 |
IPlacementConstraints placementConstraints, |
70 |
double cartographicSymbolSize, Cancellable cancel, ViewPort vp) {
|
71 |
|
72 |
if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES; |
73 |
FShape shp = (FShape)geom.getInternalShape(); |
74 |
Geometry geo = FConverter.java2d_to_jts(shp); |
75 |
double theta = 0; |
76 |
if (geo == null) { |
77 |
return CannotPlaceLabel.NO_PLACES;
|
78 |
} |
79 |
Point pJTS;
|
80 |
if(placementConstraints.isFitInsidePolygon()){
|
81 |
pJTS = geo.getInteriorPoint(); |
82 |
if (pJTS == null) { |
83 |
Logger.getAnonymousLogger().log(Level.SEVERE, "no interior point could be found"); |
84 |
return CannotPlaceLabel.NO_PLACES;
|
85 |
} |
86 |
} else {
|
87 |
pJTS = geo.getCentroid(); |
88 |
|
89 |
if (pJTS == null) { |
90 |
Logger.getAnonymousLogger().log(Level.SEVERE, "no centroid could be found"); |
91 |
return CannotPlaceLabel.NO_PLACES;
|
92 |
} |
93 |
} |
94 |
|
95 |
Point2D startingPoint = new Point2D.Double(pJTS.getX(), pJTS.getY()); |
96 |
|
97 |
// calculated with the Linear Regression technique
|
98 |
PathIterator pi = shp.getPathIterator(null); |
99 |
Rectangle geomBounds = shp.getBounds();
|
100 |
double sumx = 0, sumy = 0, sumxx = 0, sumyy = 0, sumxy = 0; |
101 |
double Sxx, Sxy, b, a;
|
102 |
double[] coords = new double[6]; |
103 |
int count = 0; |
104 |
|
105 |
// add points to the regression process
|
106 |
Vector<Point2D> v = new Vector<Point2D>(); |
107 |
while (!pi.isDone()) {
|
108 |
pi.currentSegment(coords); |
109 |
Point2D p;
|
110 |
if (geomBounds.width > geomBounds.height)
|
111 |
p = new Point2D.Double(coords[0], coords[1]); |
112 |
else
|
113 |
p = new Point2D.Double(coords[1], coords[0]); |
114 |
v.addElement(p); |
115 |
count++; |
116 |
sumx += p.getX(); |
117 |
sumy += p.getY(); |
118 |
sumxx += p.getX()*p.getX(); |
119 |
sumyy += p.getY()*p.getY(); |
120 |
sumxy += p.getX()*p.getY(); |
121 |
pi.next(); |
122 |
} |
123 |
|
124 |
// start regression
|
125 |
double n = (double) count; |
126 |
Sxx = sumxx-sumx*sumx/n; |
127 |
Sxy = sumxy-sumx*sumy/n; |
128 |
b = Sxy/Sxx; |
129 |
a = (sumy-b*sumx)/n; |
130 |
|
131 |
boolean isVertical = false; |
132 |
if (geomBounds.width < geomBounds.height) {
|
133 |
if (b == 0) { |
134 |
// force vertical (to avoid divide by zero)
|
135 |
isVertical = true;
|
136 |
|
137 |
} else {
|
138 |
// swap axes
|
139 |
double bAux = 1/b; |
140 |
a = - a / b; |
141 |
b = bAux; |
142 |
} |
143 |
} |
144 |
|
145 |
if (isVertical){
|
146 |
theta = AbstractLinePlacement.HALF_PI; |
147 |
} else {
|
148 |
double p1x = 0; |
149 |
double p1y =geomBounds.height-a;
|
150 |
double p2x = geomBounds.width;
|
151 |
double p2y = geomBounds.height-
|
152 |
(a+geomBounds.width*b); |
153 |
|
154 |
theta = -Math.atan(((p2y - p1y) / (p2x - p1x)) );
|
155 |
} |
156 |
|
157 |
ArrayList<LabelLocationMetrics> guessed = new ArrayList<LabelLocationMetrics>(); |
158 |
Rectangle labelBounds = lc.getBounds();
|
159 |
double cosTheta = Math.cos(theta); |
160 |
double sinTheta = Math.sin(theta); |
161 |
double halfHeight = labelBounds.getHeight()*0.5; |
162 |
double halfWidth= labelBounds.getWidth()*0.5; |
163 |
double offsetX = halfHeight * sinTheta + halfWidth*cosTheta;
|
164 |
double offsetY = -halfHeight * cosTheta + halfWidth*sinTheta;
|
165 |
double offsetRX=vp.toMapDistance((int)offsetX); |
166 |
double offsetRY=vp.toMapDistance((int)offsetY); |
167 |
startingPoint.setLocation(startingPoint.getX() - offsetRX, |
168 |
startingPoint.getY() - offsetRY); |
169 |
FPoint2D p=(FPoint2D)FConverter.transformToInts(ShapeFactory.createPoint2D(startingPoint.getX(),startingPoint.getY()), vp.getAffineTransform()); |
170 |
|
171 |
guessed.add(new LabelLocationMetrics(new Point2D.Double(p.getX(),p.getY()), -theta, true)); |
172 |
return guessed;
|
173 |
} |
174 |
public boolean isSuitableFor(IPlacementConstraints placementConstraints, |
175 |
int shapeType) {
|
176 |
if ((shapeType % FShape.Z) == FShape.POLYGON) {
|
177 |
return placementConstraints.isParallel();
|
178 |
} |
179 |
return false; |
180 |
} |
181 |
|
182 |
} |