Exemplo n.º 1
0
            public void TestAffineTransform2D()
            {
                //Setup some affine transformation
                System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
                matrix.RotateAt(30, new System.Drawing.PointF(0, 0));
                matrix.Translate(-20, -20, System.Drawing.Drawing2D.MatrixOrder.Append);
                matrix.Shear(0.95f, -0.2f, System.Drawing.Drawing2D.MatrixOrder.Append);

                //Create some random sample data
                CreatingData cd = new CreatingData();
                SharpMap.Data.FeatureDataTable fdt1 =
                cd.CreatePointFeatureDataTableFromArrays(GetRandomOrdinates(80, -180, 180),
                                                         GetRandomOrdinates(80, -90, 90), null);

                //Clone random sample data and apply affine transformation on it
                SharpMap.Data.FeatureDataTable fdt2 = TransformedFeatureDataTable(matrix, fdt1);

                //Get affine transformation with LeastSquaresTransform
                SharpMap.Utilities.LeastSquaresTransform lst = new SharpMap.Utilities.LeastSquaresTransform();

                //Add at least three corresponding points
                lst.AddInputOutputPoint(
                ((SharpMap.Data.FeatureDataRow)fdt1.Rows[0]).Geometry as SharpMap.Geometries.Point,
                ((SharpMap.Data.FeatureDataRow)fdt2.Rows[0]).Geometry as SharpMap.Geometries.Point);

                lst.AddInputOutputPoint(
                ((SharpMap.Data.FeatureDataRow)fdt1.Rows[39]).Geometry as SharpMap.Geometries.Point,
                ((SharpMap.Data.FeatureDataRow)fdt2.Rows[39]).Geometry as SharpMap.Geometries.Point);

                lst.AddInputOutputPoint(
                ((SharpMap.Data.FeatureDataRow)fdt1.Rows[79]).Geometry as SharpMap.Geometries.Point,
                ((SharpMap.Data.FeatureDataRow)fdt2.Rows[79]).Geometry as SharpMap.Geometries.Point);

                /*
                //Get affine transformation calculates mean points to improve accuaracy
                //Unfortunately the result is not very good, so, since I know better I manually set these
                //mean points.
                lst.SetMeanPoints(new SharpMap.Geometries.Point(0, 0),
                              new SharpMap.Geometries.Point(matrix.OffsetX, matrix.OffsetY));
                 */

                //Create Affine
                AffineCoordinateTransformation2D at2 = new AffineCoordinateTransformation2D(lst.GetAffineTransformation());

                //Create Map
                SharpMap.Map map = new SharpMap.Map(new System.Drawing.Size(720, 360));

                //Add not transformed layer
                map.Layers.Add(new SharpMap.Layers.VectorLayer("L1",
                                                           new SharpMap.Data.Providers.GeometryFeatureProvider(fdt1)));
                ((SharpMap.Layers.VectorLayer) map.Layers[0]).Style.Symbol =
                new System.Drawing.Bitmap(@"..\..\..\DemoWinForm\Resources\flag.png");

                //Add transformed layer
                map.Layers.Add(new SharpMap.Layers.VectorLayer("L2",
                                                           new SharpMap.Data.Providers.GeometryFeatureProvider(fdt2)));
                ((SharpMap.Layers.VectorLayer) map.Layers[1]).Style.Symbol =
                new System.Drawing.Bitmap(@"..\..\..\DemoWinForm\Resources\women.png");

                //Render map
                map.ZoomToExtents();

                //Get map and save to file
                var bmp = (System.Drawing.Bitmap)map.GetMap();
                bmp.Save("affinetransform1.bmp");

                //we want to reverse the previously applied transformation.
                ((SharpMap.Layers.VectorLayer) map.Layers[1]).CoordinateTransformation = (AffineCoordinateTransformation2D)at2.Inverse();

                //Render map
                map.ZoomToExtents();

                //Get map and save to file
                bmp = (System.Drawing.Bitmap)map.GetMap();
                bmp.Save("affinetransform2.bmp");
                //Hopefully women cover flags ;-).
            }
Exemplo n.º 2
0
        public void TestAffineTransform2D()
        {
            //Setup some affine transformation
            System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
            matrix.RotateAt(30, new System.Drawing.PointF(0, 0));
            matrix.Translate(-20, -20, System.Drawing.Drawing2D.MatrixOrder.Append);
            matrix.Shear(0.95f, -0.2f, System.Drawing.Drawing2D.MatrixOrder.Append);
            
            //Create some random sample data
            SharpMap.Data.FeatureDataTable fdt1 =
                CreatingData.CreatePointFeatureDataTableFromArrays(GetRandomOrdinates(80, -180, 180),
                                                                   GetRandomOrdinates(80, -90, 90), null);

            //Clone random sample data and apply affine transformation on it
            SharpMap.Data.FeatureDataTable fdt2 = TransformedFeatureDataTable(matrix, fdt1);

            //Get affine transformation with LeastSquaresTransform
            SharpMap.Utilities.LeastSquaresTransform lst = new SharpMap.Utilities.LeastSquaresTransform();

            //Add at least three corresponding points
            lst.AddInputOutputPoint(
                ((SharpMap.Data.FeatureDataRow)fdt1.Rows[0]).Geometry.Coordinate,
                ((SharpMap.Data.FeatureDataRow)fdt2.Rows[0]).Geometry.Coordinate);

            lst.AddInputOutputPoint(
                ((SharpMap.Data.FeatureDataRow)fdt1.Rows[39]).Geometry.Coordinate,
                ((SharpMap.Data.FeatureDataRow)fdt2.Rows[39]).Geometry.Coordinate);

            lst.AddInputOutputPoint(
                ((SharpMap.Data.FeatureDataRow)fdt1.Rows[79]).Geometry.Coordinate,
                ((SharpMap.Data.FeatureDataRow)fdt2.Rows[79]).Geometry.Coordinate);

            /*
            //Get affine transformation calculates mean points to improve accuaracy
            //Unfortunately the result is not very good, so, since I know better I manually set these
            //mean points.
            lst.SetMeanPoints(new GeoAPI.Geometries.IPoint(0, 0), 
                              new GeoAPI.Geometries.IPoint(matrix.OffsetX, matrix.OffsetY));
             */

            //Create Affine
            AffineCoordinateTransformation2D at2 = new AffineCoordinateTransformation2D(lst.GetAffineTransformation());

            //Create Map
            SharpMap.Map map = new SharpMap.Map(new System.Drawing.Size(720, 360));

            //Add not transformed layer
            map.Layers.Add(new SharpMap.Layers.VectorLayer("L1",
                                                           new SharpMap.Data.Providers.GeometryFeatureProvider(fdt1)));
            ((SharpMap.Layers.VectorLayer) map.Layers[0]).Style.Symbol =
                new System.Drawing.Bitmap(@"..\..\..\DemoWinForm\Resources\flag.png");

            //Add transformed layer
            map.Layers.Add(new SharpMap.Layers.VectorLayer("L2",
                                                           new SharpMap.Data.Providers.GeometryFeatureProvider(fdt2)));
            ((SharpMap.Layers.VectorLayer) map.Layers[1]).Style.Symbol =
                new System.Drawing.Bitmap(@"..\..\..\DemoWinForm\Resources\women.png");

            //Render map
            map.ZoomToExtents();

            //Get map and save to file
            var bmp = (System.Drawing.Bitmap)map.GetMap();
            bmp.Save("affinetransform1.bmp");

            //we want to reverse the previously applied transformation.
            ((SharpMap.Layers.VectorLayer) map.Layers[1]).CoordinateTransformation = (AffineCoordinateTransformation2D)at2.Inverse();

            //Render map
            map.ZoomToExtents();

            //Get map and save to file
            bmp = (System.Drawing.Bitmap)map.GetMap();
            bmp.Save("affinetransform2.bmp");
            //Hopefully women cover flags ;-).

        }