Beispiel #1
0
        public static bool Transform(this DMesh3 dMesh, SpatialReference to)
        {
            string crs = dMesh.FindMetadata("CRS") as string;

            if (crs != null && crs != "")
            {
                SpatialReference from = new SpatialReference(null);
                if (crs.Contains("+proj"))
                {
                    from.ImportFromProj4(crs);
                }
                else if (crs.StartsWith("epsg") || crs.StartsWith("EPSG"))
                {
                    int epsg = int.Parse(crs.Split(':')[1]);
                    from.ImportFromEPSG(epsg);
                }
                else
                {
                    from.ImportFromWkt(ref crs);
                };
                try {
                    CoordinateTransformation trans = new CoordinateTransformation(from, to);
                    for (int i = 0; i < dMesh.VertexCount; i++)
                    {
                        if (dMesh.IsVertex(i))
                        {
                            Vector3d vertex = dMesh.GetVertex(i);
                            double[] dV     = new double[3] {
                                vertex.x, vertex.y, vertex.z
                            };
                            trans.TransformPoint(dV);
                            AppState.instance.mapTrans.TransformPoint(dV);
                            dMesh.SetVertex(i, new Vector3d(dV));
                        }
                    }
                    ;
                    return(true);
                } catch {
                    return(false);
                }
            }
            try {
                for (int i = 0; i < dMesh.VertexCount; i++)
                {
                    if (dMesh.IsVertex(i))
                    {
                        Vector3d vertex = dMesh.GetVertex(i);
                        double[] dV     = new double[3] {
                            vertex.x, vertex.y, vertex.z
                        };
                        AppState.instance.mapTrans.TransformPoint(dV);
                        dMesh.SetVertex(i, new Vector3d(dV));
                    }
                }
                ;
                return(true);
            } catch {
                return(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// DotSpatial to Ogr at projection level
        /// </summary>
        /// <param name="projectionInfo">DotSpatial ProjectionInfo</param>
        /// <returns>Ogr SpatialReference</returns>
        public static OSGeo.OSR.SpatialReference DS2OgrProjection(DotSpatial.Projections.ProjectionInfo projectionInfo)
        {
            string proj4 = projectionInfo.ToProj4String();

            OSGeo.OSR.SpatialReference spatialReference = new OSGeo.OSR.SpatialReference("");
            spatialReference.ImportFromProj4(proj4);
            return(spatialReference);
        }
Beispiel #3
0
 public static SpatialReference TextToSR(string str)
 {
     if (str.Contains("epsg:") || str.Contains("EPSG:"))
     {
         SpatialReference crs   = new SpatialReference(null);
         string[]         parts = str.Split(':');
         crs.ImportFromEPSG(int.Parse(parts[1]));
         return(crs);
     }
     if (str.Contains("proj"))
     {
         SpatialReference crs = new SpatialReference(null);
         crs.ImportFromProj4(str);
         return(crs);
     }
     return(new SpatialReference(str));
 }
Beispiel #4
0
        // export map as raster-image
        private void renderButton_Click(object sender, EventArgs e)
        {
            toolBuilder.addHeader("Export to GeoTiff", false);
            // textbox for increase in resolution
            var zoom = toolBuilder.addTextboxWithCaption("Zoom factor:", "1");
            // textbox and button for new file name input
            var file = toolBuilder.addTextboxWithCaption("Filename:", "");
            var browsebutton = toolBuilder.addButton("Browse...");
            browsebutton.Click += (o, w) =>
            {
                 SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                 saveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp"  ;
                 if(saveFileDialog1.ShowDialog() == DialogResult.OK)
                     file.Text = saveFileDialog1.FileName;
            };
            // label for errors
            var error = toolBuilder.addErrorLabel();

            // button for performing rasterization
            Button button = toolBuilder.addButton("Export", (Layer selectedLayer) =>
            {
                // no filename is given
                if (file.Text == "")
                {
                    toolBuilder.setError("Please provide filename");
                    return;
                }
                // æøå not accepted in filename, not supported by GDAL
                if (file.Text.ToLower().IndexOfAny("æøå".ToCharArray()) > -1)
                {
                    toolBuilder.setError("No æøå in filename");
                    return;
                }
                double zoomfactor = 1;
                if (!double.TryParse(zoom.Text, out zoomfactor))
                {
                    // zoom factor must be a number
                    toolBuilder.setError("Zoom factor not a number");
                    return;
                }

                // calulate image resolution and new real world coordinates
                var oldWindowRect = ScreenManager.WindowsRect.Clone();
                ScreenManager.WindowsRect = new ScreenManager.SGISEnvelope(0, oldWindowRect.MaxX * zoomfactor, 0, oldWindowRect.MaxY * zoomfactor);
                ScreenManager.Calculate();
                Bitmap mapTemp = new Bitmap((int)ScreenManager.WindowsRect.Width,
                                            (int)ScreenManager.WindowsRect.Height);
                var mapRectTemp = ScreenManager.MapScreenToReal(ScreenManager.WindowsRect);
                OgcCompliantGeometryFactory fact = new OgcCompliantGeometryFactory();
                var boundingGeometry = fact.ToGeometry(mapRectTemp);
                Render render = new Render(ScreenManager.Scale, ScreenManager.Offset);
                ScreenManager.WindowsRect.Set(oldWindowRect);
                ScreenManager.Calculate();

                // background worker for performing rasterisation in another thread
                BackgroundWorker bwRender = new BackgroundWorker();
                bwRender.DoWork += (obj, args) =>
                {
                    var mapGraphics = Graphics.FromImage(mapTemp);

                    // draw background maps
                    foreach (Photo p in photos)
                    {
                        // only if visible
                        if (p.Geometry.Intersects(boundingGeometry))
                            render.Draw(p, mapGraphics);
                    }
                    // draw layers
                    foreach (Layer l in Layers.Reverse())
                    {
                        // only if visible
                        if (!l.Visible)
                            continue;
                        // draw only visible features
                        var visibleFeatures = l.getWithin(boundingGeometry);
                        lock (l) // lock layer to prevent multithreaded access to style when drawing
                        {
                            // render feature
                            foreach (Feature s in visibleFeatures)
                            {
                                render.Draw(s.Geometry, mapGraphics, l.Style);
                            }
                        }
                    }
                };
                // georeference drawn bitmap
                bwRender.RunWorkerCompleted += (obj, args) =>
                {
                    // remove .bmp ending if present
                    if (file.Text.EndsWith(".bmp"))
                        file.Text = file.Text.Substring(0, file.Text.Length-4);
                    // save render
                    mapTemp.Save(file.Text+".bmp");

                   // init GDAL and copy image
                   OSGeo.GDAL.Gdal.AllRegister();
                   OSGeo.GDAL.Driver srcDrv = OSGeo.GDAL.Gdal.GetDriverByName("GTiff");
                   OSGeo.GDAL.Dataset srcDs = OSGeo.GDAL.Gdal.Open(file.Text+".bmp", OSGeo.GDAL.Access.GA_ReadOnly);
                   OSGeo.GDAL.Dataset dstDs = srcDrv.CreateCopy(file.Text+".tiff", srcDs, 0, null, null, null);

                   //Set the map projection
                    {
                        OSGeo.OSR.SpatialReference oSRS = new OSGeo.OSR.SpatialReference("");
                        oSRS.ImportFromProj4( SRS.ToString() );
                        string wkt;
                        // convert projection to wkt
                        oSRS.ExportToWkt(out wkt);
                        dstDs.SetProjection(wkt);
                   }

                   //Set the map coordinates
                   double mapWidth = mapRectTemp.Width;
                   double mapHeight = mapRectTemp.Height;
                   double[] geoTransfo = new double[] { mapRectTemp.MinX, mapWidth / mapTemp.Width, 0, mapRectTemp.MaxY, 0, -mapHeight / mapTemp.Height };
                   dstDs.SetGeoTransform(geoTransfo);

                   dstDs.FlushCache();
                   dstDs.Dispose();
                   srcDs.Dispose();
                   srcDrv.Dispose();

                    /////////////////////////
                };
                bwRender.RunWorkerAsync();
            });
        }
Beispiel #5
0
        // returns wkt-representation of current srs
        public string getSrsName()
        {
            OSGeo.OSR.SpatialReference oSRS = new OSGeo.OSR.SpatialReference("");
            oSRS.ImportFromProj4(SRS.ToString());

            if (oSRS.IsProjected() == 1)
                return oSRS.GetAttrValue("projcs", 0);
            return oSRS.GetAttrValue("geogcs", 0);
        }