Statistics
| Revision:

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 / AbstractLinePlacement.java @ 41162

History | View | Annotate | Download (4.99 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.geom.Point2D;
44
import java.awt.geom.Rectangle2D;
45
import java.util.ArrayList;
46

    
47
import org.apache.batik.ext.awt.geom.PathLength;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.mapcontext.ViewPort;
50
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
51
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.LabelLocationMetrics;
53
import org.gvsig.tools.task.Cancellable;
54

    
55
/**
56
 *
57
 * AbstractLinePlacement.java
58
 *
59
 *
60
 * @author jaume dominguez faus - jaume.dominguez@iver.es Dec 17, 2007
61
 *
62
 */
63
public abstract class AbstractLinePlacement implements ILabelPlacement {
64
        public static final double PI = Math.PI;
65
        public static final double HALF_PI = PI * 0.5;
66
        private ArrayList<LabelLocationMetrics> guessed =
67
                        new ArrayList<LabelLocationMetrics>();
68
        private static final double TOLERANCE = 1E-2;
69

    
70

    
71
        public ArrayList<LabelLocationMetrics> guess(ILabelClass lc, Geometry geom,
72
                        IPlacementConstraints pc, double cartographicSymbolSize,
73
                        Cancellable cancel, ViewPort vp) {
74
                
75
                guessed.clear();
76

    
77
                Geometry shp = geom.cloneGeometry();
78
                shp.transform(vp.getAffineTransform());
79

    
80
                PathLength pathLen = new PathLength(shp);
81

    
82
                LabelLocationMetrics initial = initialLocation(lc, pc, pathLen, cancel);
83

    
84
                if (cancel.isCanceled()) return CannotPlaceLabel.NO_PLACES;
85

    
86
                double theta = initial.getRotation();
87

    
88

    
89
                double xOffset = 0;
90
                double yOffset = 0;
91
                Rectangle2D bounds = lc.getBounds();
92

    
93
                if (pc.isPageOriented()) {
94
                        if(theta >  HALF_PI) { // from origin to left-down
95
                                theta = (theta - Math.PI);
96

    
97
                                Point2D anchor = initial.getAnchor();
98

    
99
                                double cosTheta = Math.cos(theta);
100
                                double sinTheta = Math.sin(theta);
101
                                double width = bounds.getWidth();
102
                                double height = bounds.getHeight()*1.1;
103
                initial.getAnchor().setLocation(
104
                    anchor.getX() - cosTheta*width + sinTheta*height,
105
                    anchor.getY() - sinTheta*width - cosTheta*height);
106
                        } else if (theta < -HALF_PI ) { // from origin to left-up
107
                            /*
108
                                if (Math.abs(Math.abs(theta) - HALF_PI) -TOLERANCE > 0)
109
                                        // the condition avoids errors when segment is almost up-down vertical
110
                                {
111
                                */
112
                                        theta = (theta + Math.PI);
113
                                        Point2D anchor = initial.getAnchor();
114
                                        double cosTheta = Math.cos(theta);
115
                                        double sinTheta = Math.sin(theta);
116
                                        double width = bounds.getWidth();
117
                                        double height = bounds.getHeight()*1.1;
118
                                        initial.getAnchor().setLocation(
119
                                                        anchor.getX() - cosTheta*width + sinTheta*height,
120
                                                        anchor.getY() - sinTheta*width - cosTheta*height);
121
                                                        
122
                                // }
123
                        }
124

    
125

    
126
                }
127

    
128
                if (pc.isBelowTheLine() ||
129
                                pc.isOnTheLine() ||
130
                                pc.isAboveTheLine()) {
131

    
132
                        double h = bounds.getHeight()*0.5;
133

    
134
                        xOffset += h * Math.sin(theta);
135
                        yOffset += h * Math.cos(theta);
136

    
137

    
138
                        Point2D initialAnchor = initial.getAnchor();
139
                        // calculate the possibles in the inverse
140
                        // order to the preferred order and add it
141
                        // always in the first position
142
                        if (pc.isBelowTheLine()) {
143
                                Point2D anchor = new Point2D.Double(
144
                                                initialAnchor.getX() +        -xOffset,
145
                                                initialAnchor.getY() +        +yOffset);
146
                                guessed.add(0, new LabelLocationMetrics(anchor, theta, false));
147
                        }
148

    
149
                        if (pc.isOnTheLine()) {
150
                                guessed.add(0, new LabelLocationMetrics(initial.getAnchor(), theta, false));
151
                        }
152

    
153
                        if (pc.isAboveTheLine()) {
154
                                Point2D anchor = new Point2D.Double(
155
                                                initialAnchor.getX() +        +xOffset,
156
                                                initialAnchor.getY() +  -yOffset);
157
                                guessed.add(0, new LabelLocationMetrics(anchor, theta, false));
158
                        }
159
                } else {
160
                        // will say that on the line is legal
161
                        guessed.add(0, initial);
162
                }
163
                return guessed;
164
        }
165

    
166
        abstract LabelLocationMetrics initialLocation(
167
                        ILabelClass lc, IPlacementConstraints pc,
168
                        PathLength pathLen, Cancellable cancel);
169

    
170
}