sharpMap学习笔记之二

GIS一般都具有属性查询和空间查询的功能,今天又抽空用sharpMap在之一的基础上写了个基本属性信息查询的实例,实现的功能就是通过选定图层,输入查询条件,查询所选图层中满足条件的要素,并显示基本信息。以下是效果图:
sharpMap学习笔记之二_第1张图片
以下是源代码
///////////////////////////////////////
按钮点击事件
protected void btQuery_Click(object sender, EventArgs e)              //属性查询
    {
        if (this.txtQuery.Text.Trim() == "") return;
        //获取图层名
        string lyrName = this.DDLayers.Items[this.DDLayers.SelectedIndex].Value+".shp";
        if (szmap == null) return;
        //获取图层
        SharpMap.Layers.VectorLayer lyr = szmap.GetLayerByName(lyrName) as SharpMap.Layers.VectorLayer ;
        if (lyr == null) return;
        if (!lyr.DataSource.IsOpen) lyr.DataSource.Open();
        //获取图层的所有元素
        //法一:通过获取地图全图范围内的要素,也就是所有的要素,这种方法需要执行几何要素的相交判断
        //SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
        //lyr.DataSource.ExecuteIntersectionQuery(lyr.Envelope, ds);
        //法二:为shapefile类添加一个方法GetFeaturesDataSet(),直接获取所有的要素,不需要进行几何要素的相交判断
        SharpMap.Data.Providers.ShapeFile shapefile = (SharpMap.Data.Providers.ShapeFile)lyr.DataSource;
        SharpMap.Data.FeatureDataSet ds = shapefile.GetFeaturesDataSet();

        lyr.DataSource.Close();
        if (ds == null) return;
        System.Data.DataTable dt=new DataTable();
        System.Data.DataColumn dc=new DataColumn("名字",typeof(System.String));
        dt.Columns.Add(dc);
        foreach (SharpMap.Data.FeatureDataRow row in ds.Tables[0])
        {
            if (row["NAME"].ToString().Contains(this.txtQuery.Text.Trim()))
            {
                System.Data.DataRow r = dt.NewRow();
                r[0] = row["NAME"].ToString();
                dt.Rows.Add(r);
            }
        }
        this.gvResults.DataSource = dt;
        this.gvResults.DataBind();
        //重新生成地图
        this.GenerateMap();
    }
对于法二,我在sharpMap的ShapeFile类中添加了一个方法以获取全部要素
 public FeatureDataSet GetFeaturesDataSet()
        {
            FeatureDataSet featDataSet = new FeatureDataSet();
            FeatureDataTable featDataTable = null;
            if(!this.IsOpen )
                throw (new ApplicationException("An attempt was made to read from a closed datasource"));
            if(this.dbaseFile ==null)
                throw (new ApplicationException("An attempt was made to read DBase data from a shapefile without a valid .DBF file"));
            //获取feature数量
            int featCount = this.GetFeatureCount();
            featDataTable = this.dbaseFile.NewTable;
            //获取feature
            for (uint i = 0; i < featCount; i++)
            {
                FeatureDataRow r= this.GetFeature(i,featDataTable);
                featDataTable.AddRow(r);
            }
            featDataSet.Tables.Add(featDataTable);
            return featDataSet ;
        }

实例均是在上一个学习笔记的基础上做的,这里的属性查询功能比较简单,主要提供查询的原理,如果有必要,可以通过需要进行相应的改写。
powered by SharpMap v0.9


你可能感兴趣的:(学习笔记)