Problem with Raster.PixelToMap

393
0
12-31-2021 05:05 PM
BerndtNording
New Contributor III

I wrote a bit of code to show me the extent (footprint) of a raster with a Polyline. It should be fairly simple by getting the map coordinates of four corner pixels of the raster. Yes, I'm aware that those should give me the coordinates of the center of those pixels, but that's fine. The code works fine for Rasters with orthogonal georeferencing, but has problems if the georeferencing rotates and stretches the image.

See screenshot below for the results I'm getting

       await QueuedTask.Run(() =>
        {
          pOutLay = (FeatureLayer)LayerFactory.Instance.CreateLayer(new Uri(url), MapView.Active.Map);
          pSpatRef = SpatialReferenceBuilder.CreateSpatialReference(32655); //UTM55N
          EditOperation createFeatEditOp = new EditOperation();
          createFeatEditOp.Name = "CreateImageEdges";
          createFeatEditOp.SelectModifiedFeatures = false;
          createFeatEditOp.SelectNewFeatures = false;

          foreach (Item itm in items)
          {
            lstPnt = new List<MapPoint>();
            string path = itm.Path.Substring(0, itm.Path.LastIndexOf(@"\"));
            string strFile = itm.Path.Substring(itm.Path.LastIndexOf(@"\") + 1);
            FileSystemConnectionPath connectionPath = new FileSystemConnectionPath(new System.Uri(path), FileSystemDatastoreType.Raster);
            FileSystemDatastore dataStore = new FileSystemDatastore(connectionPath);
            RasterDataset fileRasterDataset = dataStore.OpenDataset<RasterDataset>(strFile);
            Raster pRas = fileRasterDataset.CreateDefaultRaster();
            pTup = pRas.PixelToMap(0, 0);
            lstPnt.Add(MapPointBuilder.CreateMapPoint(pTup.Item1, pTup.Item2));
            pTup = pRas.PixelToMap(0, pRas.GetWidth());
            lstPnt.Add(MapPointBuilder.CreateMapPoint(pTup.Item1, pTup.Item2));
            pTup = pRas.PixelToMap(pRas.GetHeight(), pRas.GetWidth());
            lstPnt.Add(MapPointBuilder.CreateMapPoint(pTup.Item1, pTup.Item2));
            pTup = pRas.PixelToMap(pRas.GetHeight(), 0);
            lstPnt.Add(MapPointBuilder.CreateMapPoint(pTup.Item1, pTup.Item2));
            pTup = pRas.PixelToMap(0, 0);
            lstPnt.Add(MapPointBuilder.CreateMapPoint(pTup.Item1, pTup.Item2));


            pPline = PolylineBuilder.CreatePolyline(lstPnt, pSpatRef);
            dctAtts = new Dictionary<string, object>();
            dctAtts.Add("SHAPE", pPline);
            dctAtts.Add("Image", strFile);
            dctAtts.Add("Width", pRas.GetWidth());
            dctAtts.Add("Height", pRas.GetHeight());
            createFeatEditOp.Create(pOutLay, dctAtts);
          }
          createFeatEditOp.Execute();

        });
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

The size of this image is 8272x6200 and you can see in the table that I'm not getting the correct height from the Raster object either.

Any idea what's going on here?

ScreenCap.jpgScreenCap2.jpg

0 Kudos
0 Replies