1

I'm trying to implement a mouse_move event handler to display the elevation based on the mouse location. I'm using the ThinkGeo API to display the raster map (.tif) and gdal C# to get the elevation data from Dted file. Solve issues that for out of range. But after i play around with the map it suddenly throws :

ArgumentOutOfRangeException was Unhandled

with message:

The input double value is out of range.

What this is means?

WORKING CODE(EDIT)

  private void Map1_MouseMove(object sender, MouseEventArgs e)

        {

            Point point = e.MouseDevice.GetPosition(Map1);

            ScreenPointF screenPointF = new ScreenPointF((float)point.X, (float)point.Y);
            PointShape pointShape = ExtentHelper.ToWorldCoordinate(Map1.CurrentExtent, screenPointF, (float)Map1.ActualWidth, (float)Map1.ActualHeight);


            string x = pointShape.X.ToString("f6", CultureInfo.InvariantCulture);
            string y = pointShape.Y.ToString("f6", CultureInfo.InvariantCulture);

            if(proj4Projection != null)
            {
                //PointShape p = new PointShape(605770.08015, 388756.038425);
                PointShape p2 = proj4Projection.ConvertToExternalProjection(pointShape) as PointShape;


                Gdal.AllRegister(); //Register driver(s).    

                //string Dted = @"C:\N03.dt2";
                double[] gGeoTrans = new double[6];
                Dataset ds = Gdal.Open(@"C:\N03.dt2", Access.GA_ReadOnly); //Open Dataset

                ds.GetGeoTransform(gGeoTrans);
                OSGeo.GDAL.Band gThisBand = ds.GetRasterBand(1);


                double X = Convert.ToDouble(p2.X);

                double Y = Convert.ToDouble(p2.Y);

                // Your query location is X, Y (both doubles), convert your location into
                // local row, col addressing to specify which cell to read
                int row = (int)((gGeoTrans[3] - Y) / -gGeoTrans[5]);// ULY - xcoord / -N-S CellSize;
                int col = (int)((X - gGeoTrans[0]) / gGeoTrans[1]);  // xcoord - ULX / E-W CellSize

                // check here that the row and col are >= 0 and < gDataset.RasterXSize and 
                // gDataset.RasterYSize or you will get a read error.

                // read the value.. reading is done to an array but as we want only one cell
                // it needs to be an array of 1 float.
                float[] buff = new float[1];

                 if ((col <= ds.RasterXSize) && col >= 0)

                 { if ((row <= ds.RasterYSize) && row >= 0)
                    {
                        gThisBand.ReadRaster(col, row, 1, 1, buff, 1, 1, 0, 0); // read the cell value
                     } 
                 } 
                 else 
                 { 
                    return;
                 }

                 float ThisCellValue = buff[0];

                TextBoxSatu.Text = "RSO_WEST(KERTAU)" + "(" + (x) + "," + " " + (y) + ")" + " " + "Long: " + DecimalDegreesHelper.GetDegreesMinutesSecondsStringFromDecimalDegree(p2.X) + " Lat: " + DecimalDegreesHelper.GetDegreesMinutesSecondsStringFromDecimalDegree(p2.Y) + " " + "Height :" +ThisCellValue +"m" ;
  • You're outside your raster with your row and column, try step debugging and see where you get a value for row and column that is outside the range for your raster (0 to GetXSize(), 0 to GetYSize()). It may be an incorrect projection or a swapping of lat and lon. I'm not versed in the GeoThink API to discern your coordinate transformation. – Michael Stimson Dec 17 '18 at 2:11
  • I debug it, it like you said it throw error if it out of range. The range for both row and column must be in this range (row =>0 && row=<3601). The 3601 is the value get from ds.rastersize. @MichaelStimson do you know how i can make it not throw error even I out of range? For example if it out of range it will show Height=undefined m. In addtiion, i already change the projection for map same to project of Dted file. – Mohamad Fathin Dec 18 '18 at 1:20
  • I didn't include it in the code sample for your other question gis.stackexchange.com/questions/305774/… but I use a simple if statement if ((col < gDataset.RasterXSize) && col >= 0){ if ((row < gDataset.RasterYSize) && row >= 0){ .. do the work here .. } } else { pForm.PickedValue.Text = "cell is outside raster"; pForm.Update();} to trap when the clicked coordinate is outside the raster; pForm is the associated form and PickedValue is a textbox. – Michael Stimson Dec 18 '18 at 1:29
  • @MichaelStimson this pForm function is for the Windoform right? Did you know how can i use on WPF? – Mohamad Fathin Dec 18 '18 at 4:39
  • Yes pForm is a WinForm and PickedValue is a control on that form with a Text property, but no, I haven't done any WPF development, it's just a propagated message so I guess any mechanism would work - even a messagebox. Have a read of differencebetween.net/technology/… which might help explain. – Michael Stimson Dec 18 '18 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.