воскресенье, 24 августа 2014 г.

Rotation text in line's start point by attribute

I needed to create SLD style for lines with text rotation on angle equals line azimuth in start point of line. Like this:

Data

My table creation script:

CREATE TABLE annotations
(
  gid serial NOT NULL,
  annotation character varying(250),
  the_geom geometry,
  angle character varying(20) DEFAULT 0,
  CONSTRAINT annotations_pkey PRIMARY KEY (gid),
  CONSTRAINT enforce_dims_the_geom CHECK (st_ndims(the_geom) = 2),
  CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = 4326)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE annotations
  OWNER TO postgres;

"angle" field - contains line azimuth, to calculate it i use trigger:

CREATE TRIGGER set_angle
  BEFORE INSERT OR UPDATE
  ON annotations
  FOR EACH ROW
  EXECUTE PROCEDURE setangle();

And function:

CREATE OR REPLACE FUNCTION setangle()
  RETURNS trigger AS
$BODY$
BEGIN
   NEW.angle := degrees(
                             ST_Azimuth(
                                ST_Transform(ST_StartPoint(NEW.the_geom),900913)
                              , ST_Transform(ST_EndPoint(NEW.the_geom),900913)
                             )
                          ) - 90;
   RETURN NEW;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION setangle()
  OWNER TO postgres;

As you can notice my table SRID=4326, but in calculation points transform into SRID=900913. It's becouse my application map projection is EPSG:900913. If do not transform point to projection what you will show this table you get difference between line azimuth and angle field value:

Style

Here is my SLD style:



  
    Annotatioans
    
      Annotations
      Annotations white text
      
              
        
          
            
              the_geom
            
          
          
           
             Arial
             fontSize
             normal
             bold
           
          
           
             
               0.0
               0.5
             
             
               0
               5
             
             
              rotationAngle
             
           
          
          
            1
            
              #000000
            
          
          
            #FFFFFF
          
          10
          yes
        
        
          
            #000000
          
        
      
    
  
 

Main part here is Rotation block:


  rotationAngle


Комментариев нет:

Отправить комментарий