Objects中如何查询与线相交的栅格

作者:小诗

      在项目应用中我们总是会遇到这样一种需求:如何提取出某条线段经过的栅格数据的栅格值,小编集众人之所需,对这部分做了个总结。今天就跟着小编来看看具体是如何实现的吧。

(1) 自备一份栅格数据,将栅格数据导入到数据源。程序运行后直接加载数据源到程序中,将栅格数据加载到地图窗口;

DatasourceConnectionInfo info = new DatasourceConnectionInfo();
info.Server=@"..\..\data\data.udb";
info.EngineType = EngineType.UDB;
info.Alias = "data";
m_workspace.Datasources.Open(info);
m_mapcontrol.Map.Layers.Add(m_workspace.Datasources[0].Datasets[0], true);

(2) 通过行列和获取一个单元格的坐标点,通过Geometrist.ComputeGeodesicDistance方法获取单元格变成大小;关键代码如下:

Point2D point2d1 = datasetgrid .GridToXY (new Point (1,1));
Point2D point2d2 = datasetgrid .GridToXY (new Point (1,2)); 
Point2Ds pts = new Point2Ds();
pts.Add(point2d1);
pts.Add(point2d2);
double axis = datasetgrid.PrjCoordSys.GeoCoordSys.GeoDatum.GeoSpheroid.Axis;
double flatten = datasetgrid.PrjCoordSys.GeoCoordSys.GeoDatum.GeoSpheroid.Flatten;
bian = Geometrist.ComputeGeodesicDistance(pts, axis, flatten);

(3)单击功能执行按钮,将地图控件的绘制方式设置为在内存中绘制,并且将其操作状态设置编辑折线,即可在地图的跟踪图层内编辑需要查询栅格的线段了。通过对获取所编辑的线对象生成缓冲区分析,缓冲区半径为单元格大小。

//缓冲区分析;
private void buffer(DatasetVector sourcedataset,DatasetVector resultdataset)
{
BufferAnalystParameter parameter = new BufferAnalystParameter();
parameter.EndType = BufferEndType.Round;
parameter.LeftDistance = distance;
parameter.RightDistance = distance;
parameter.RadiusUnit = BufferRadiusUnit.Meter;
BufferAnalyst.CreateBuffer(sourcedataset, resultdataset, parameter, false, true);            
}

(4) 获取缓冲区分析得到的几何面对象,用该对象对栅格数据进行裁剪。

RasterClip.Clip(datasetgrid, georegion, true, true, m_workspace.Datasources[0], "rasterresult");

(5)创建一个新的点数据集来存储结果数据,并且添加一个字段来存储每个采样点的栅格值“value”。

//创建点数据集来存放结果;       
DatasetVector datasetpoint;
DatasetVectorInfo info = new DatasetVectorInfo();
info.Type = DatasetType.Point;
info.Name = datasetname;
datasetpoint =  m_workspace.Datasources[datasourcename].Datasets.Create(info);
FieldInfos fieldinfos = datasetpoint.FieldInfos;
 FieldInfo fieldinfo = new FieldInfo();
fieldinfo.Name = "value";
fieldinfo.Type = FieldType.Int32;
fieldinfo.MaxLength = 100;
fieldinfos.Add(fieldinfo);
return datasetpoint;

(6)第四步裁剪所得到的栅格数据集,通过循环遍历,如果栅格值不为空值,则对当前栅格构建网格面对象,将该面对象与之前所画线对象进行是否相交判断,如果相交,通过栅格数据的GetValue方法获取到对应点的栅格值,将当前记录添加到结果点数据集中即可。

for (int i = 0; i < rasterresult .Width; i++)
{
 for (int j = 0; j < rasterresult.Height; j++)
  {
    double value = rasterresult.GetValue(i, j);
    if (value != -9999)
    {                        
       Point2D point2d = rasterresult.GridToXY(new Point(i, j));
       Point2Ds pts = new Point2Ds();
       pts.Add(point2d);
       pts .Add (new Point2D (point2d .X ,point2d .Y - 0.000833333299999999));
       pts.Add(new Point2D(point2d.X + 0.000833333299999999, point2d.Y - 0.000833333299999999));
       pts.Add(new Point2D(point2d.X + 0.000833333299999999,point2d .Y ));
       GeoRegion georegion = new GeoRegion(pts); 
       if (Geometrist.HasIntersection(georegion , geoline))
         {
            recordset.MoveTo(k);
            point2d = georegion.Bounds.Center;
            GeoPoint geopoint = new GeoPoint(point2d);
            recordset.AddNew(geopoint); 
            recordset.SetObject("value", value); 
             k++;
         }
     }
 }               

好啦,就这样一步步实现啦

结果图:
Objects中如何查询与线相交的栅格_第1张图片

你可能感兴趣的:(数据,应用,栅格,相交查询)