Geometry rotation problem.

Topics: Algorithms, WinForms Controls
Jul 4, 2015 at 8:03 PM
Hi All,
i am trying to rotate my vectorlayer geometry (like original shape rotate form 0 to 30. 30 to 45, 45 to any angle), how could this possible in sharpmap ?, my sample code to create vectorlayer with geometry is in the following, please check.
private void btnAddShape_Click(object sender, EventArgs e)
        {
            ShapeInfo shapeinfo = new ShapeInfo(); 
            shapeinfo.ShapeName = "dummy_" + shapeList.Count.ToString ();
            shapeinfo.WorldCoordinate = this.worldCoordinate;
            DrawShape(shapeinfo);
            this.shapeList.Add(shapeinfo);
        }

        private void DrawShape(ShapeInfo shapeInfo)
        {

            VectorLayer contactLayer = new VectorLayer(shapeInfo.ShapeName);
            Size renderSize = new System.Drawing.Size(10, 5);
            
            var wh = new SizeF(renderSize.Width * 0.5f, renderSize.Height * 0.5f);
            Coordinate center = new Coordinate();
            center.X = shapeInfo.WorldCoordinate.X;
            center.Y = shapeInfo.WorldCoordinate.Y; 
            Coordinate lt = new Coordinate(center.X - wh.Width , center.Y + wh.Height );
            Coordinate rb = new Coordinate(center.X + wh.Width, center.Y - wh.Height);

            GeometricShapeFactory factory = new GeometricShapeFactory();
            factory.Envelope = new Envelope(lt, rb);
            IPolygon geomEllipse = factory.CreateCircle(); 

            GeometryFeatureProvider geomFeatureProvider = new GeometryFeatureProvider(geomEllipse);
            contactLayer.LayerName = shapeInfo.ShapeName;
            contactLayer.DataSource = geomFeatureProvider;
            contactLayer.Style.EnableOutline = true;
            contactLayer.Style.Outline.Color = Color.Red;

            mapImage.Map.Layers.Add(contactLayer);
            mapImage.Refresh();
        }
Developer
Jul 6, 2015 at 10:32 AM
The Map has the property MapTransform that you can use to apply any matrix transformation:
var maptransform = new System.Drawing.Drawing2D.Matrix(); //Create transformation matrix

maptransform.RotateAt(45,new PointF(myMap.Size.Width/2,myMap.Size.Height/2)); //Apply 45 degrees rotation around the center of the map

myMap.MapTransform = maptransform; //Apply transformation to map
Jul 6, 2015 at 6:24 PM
i am required to (rotate) transform my geometry object (as defined in my above code) rather background map, your code of line rotate complete map at 45 degree, but i need to rotate VectorLayer geometry only.
Developer
Jul 7, 2015 at 10:39 AM
Well, once you have defined the rotation matrix, for example like done in the previous example, rather than assign it to the map you can use it to transform the vertices of your geometries.

pseudo-code:

for-each geometry
maptransform.TransformPoints(geometry.Vertices)

you just need to pay attention because the matrix method accepts PointF but the vertices are Coordinate, you can easily extract the X,Y pair from it.
Jul 11, 2015 at 8:11 PM
thanks becio, and sorry for late reply.