Nous utilisons des cookies pour vous garantir la meilleure expérience sur notre site. Si vous continuez à utiliser ce dernier, nous considèrerons que vous acceptez l'utilisation des cookies. J'ai compris ! ou En savoir plus !.
banniere

Le portail francophone de la géomatique


Toujours pas inscrit ? Mot de passe oublié ?
Nom d'utilisateur    Mot de passe              Toujours pas inscrit ?   Mot de passe oublié ?

Annonce

Printemps des cartes 2024

#1 Tue 10 July 2007 14:04

anaxa_gore
Juste Inscrit !
Date d'inscription: 9 Jul 2007
Messages: 7

[GeoTools 2.x] Rafraichissement des labels

Bonjour,

J'affiche des villes à l'aide de GeoTools. Pour cela, j'utilise les styles SLD. Voici le style :

Code:

<UserStyle xmlns:ogc="http://www.opengis.net">
    <FeatureTypeStyle>
    
        <!-- This part specifies the point symbol -->
        <Rule>
            <PointSymbolizer>
                <Geometry>
                    <ogc:PropertyName>site_geom</ogc:PropertyName>
                </Geometry>
                <Graphic>
                    <Mark>
                        <WellKnownName>star</WellKnownName>
                        <Fill>
                            <CssParameter name="fill">#ee3b3b</CssParameter>
                            <CssParameter name="fill-opacity">1</CssParameter>
                        </Fill>
                        <Stroke>
                            <CssParameter name="stroke">#0000aa</CssParameter>
                            <CssParameter name="stroke-opacity">1</CssParameter>
                            <CssParameter name="stroke-width">1</CssParameter>
                        </Stroke>
                    </Mark>
                    <Size>10.0</Size>
                </Graphic>
            </PointSymbolizer>
        </Rule>
        
        <!-- This part specifies the label name -->
        <Rule>
            <TextSymbolizer>
                <Label>
                    <ogc:PropertyName>Name</ogc:PropertyName>
                </Label>
                <LabelPlacement>
                    <PointPlacement>
                        <AnchorPoint>
                            <AnchorPointX>1.0</AnchorPointX>
                            <AnchorPointY>0.5</AnchorPointY>
                        </AnchorPoint>
                        <Displacement>
                            <DisplacementX>-10</DisplacementX>
                            <DisplacementY>5</DisplacementY>
                        </Displacement>
                    </PointPlacement>
                </LabelPlacement>
                <Font/>
                <Fill>
                    <CssParameter name="fill">#0000ff</CssParameter>"
                </Fill>
                <Halo/>
            </TextSymbolizer>
        </Rule>
    </FeatureTypeStyle>
</UserStyle>

J'utilise ensuite le StreamingRenderer pour afficher les sites que je désire placer. Le problème est que lorsque je redimensionne la fenêtre, les étoiles changent bien de position, mais les labels, eux, sont mal rafraichis, ce qui fait que je me retrouve avec des dizaines de labels sur ma carte.

Quelqu'un a-til déjà rencontré ce problème ... et surtout quelqu'un a-t-il déjà résolu ce problème ?

Merci d'avance.
TB

PS : Voici le code au moyen duquel j'affiche mes points :

Code:

public class SensorSiteStationPanel extends JMapPane {
    public static final long serialVersionUID = 1L;
    
    private final Envelope globalEnvelope;
    private final StreamingRenderer strRenderer = new StreamingRenderer();
    
    private final SensorSiteStation _1stSensorSite = new SensorSiteStation("Paris",48.859251,2.346198);
    private final SensorSiteStation _2ndSensorSite = new SensorSiteStation("Madrid",40.418285,-3.709058);
    private final SensorSiteStation _3rdSensorSite = new SensorSiteStation("Rio de Janeiro",-22.909349,-43.215028);
    
    public static void main(String[] args) throws NullPointerException, IllegalAttributeException, IOException
    {
        JFrame frm = new JFrame();
        
        frm.setContentPane(new SensorSiteStationPanel());
        
        frm.setSize(1090, 550);
        frm.setVisible(true);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setTitle("Premier site");
    }
    
    public SensorSiteStationPanel() throws NullPointerException, IllegalAttributeException, IOException
    {
        this.setSize(1080, 540);
        this.globalEnvelope = new Envelope(new Coordinate(-180,-90),new Coordinate(180,90));
        MapContext mapContext = new DefaultMapContext(DefaultGeographicCRS.WGS84);
        mapContext.setTitle("Premiers sites");
        this.setOpaque(false);
        this.setContext(mapContext);
        
        
        DefaultFeatureCollection collectionSite = 
            new DefaultFeatureCollection("Sites Collection",SensorSiteStationFeature.createSSSFeatureType(DefaultGeographicCRS.WGS84));
        
        collectionSite.add(new SensorSiteStationFeature(SensorSiteStationFeature.createSSSFeatureType(DefaultGeographicCRS.WGS84),
                _1stSensorSite));
        collectionSite.add(new SensorSiteStationFeature(SensorSiteStationFeature.createSSSFeatureType(DefaultGeographicCRS.WGS84),
                _2ndSensorSite));
        
        
        MapLayer _1stLayer = new SensorSiteStationLayer(collectionSite);
        _1stLayer.setTitle("Un premier site !!!");
        _1stLayer.setVisible(true);
        
        collectionSite.remove(_1stSensorSite);
        collectionSite.remove(_2ndSensorSite);
        collectionSite.add(new SensorSiteStationFeature(SensorSiteStationFeature.createSSSFeatureType(DefaultGeographicCRS.WGS84),
                _3rdSensorSite));
        
        MapLayer _2ndLayer = new SensorSiteStationLayer(collectionSite);
        _2ndLayer.setTitle("Un deuxieme site !!!");
        _2ndLayer.setVisible(true);
        
        mapContext.addLayer(_1stLayer);
        mapContext.addLayer(_2ndLayer);
        mapContext.setAreaOfInterest(this.globalEnvelope, DefaultGeographicCRS.WGS84);
    
        //strRenderer.setConcatTransforms(true);
        
        this.setRenderer(strRenderer);
        this.setMapArea(mapContext.getLayerBounds());
        this.setBorder(new LineBorder(Color.blue,2));
    }
    
    public void paintComponent(Graphics g)
    {
        if (isOpaque ()) {
            g.setColor (Color.white);
            g.fillRect (0, 0, getWidth (), getHeight ());
          }
          _paintComponent ((Graphics2D) g);
    }
    
    private void _paintComponent (final Graphics2D graphics2D)
    {
      graphics2D.setRenderingHints(new RenderingHints
                                              (RenderingHints.KEY_ANTIALIASING,
                                               RenderingHints.VALUE_ANTIALIAS_ON));
      graphics2D.drawImage(new ImageIcon(this.getClass().getResource("/tests/Continent_def.gif")).getImage(),
              0,0,this.getWidth(),this.getHeight(),null);
      
      final AffineTransform affineTransform
      = new AffineTransform ()
        {
          public static final long serialVersionUID = 1L;
          {
            final double scaleFactor
               = Math.min
                    ((getWidth ()/ globalEnvelope.getWidth ()),
                     (getHeight ()/ globalEnvelope.getHeight ()));
            
            System.out.println(scaleFactor);
            
            // Translate to the center of the JPanel.
            translate ((getWidth () / 2), (getHeight () / 2));

            // Scale with negative y factor to correct the orientation.
            scale (scaleFactor, - scaleFactor);

            // Translate to the center of the feature collection.
            translate
               (- ((globalEnvelope.getMinX () + globalEnvelope.getMaxX ()) / 2),
                - ((globalEnvelope.getMinY () + globalEnvelope.getMaxY ()) / 2));
          }
        };
        
        ReferencedEnvelope strRendererEnvelope = new ReferencedEnvelope(globalEnvelope,DefaultGeographicCRS.WGS84);
        
        this.strRenderer.paint(graphics2D, getBounds(), strRendererEnvelope, affineTransform);
    }
}

Hors ligne

 

Pied de page des forums

Powered by FluxBB