统计"面"要素中"点"要素的个数.

步骤

  1,创建字段 IFields

 1 /// <summary>
 2         /// 创建:"面"-"点数"的字段.
 3         /// </summary>
 4         /// <returns></returns>
 5         public static ESRI.ArcGIS.Geodatabase.IFields CreateFields() {
 6             ESRI.ArcGIS.Geodatabase.IField fieldId = new ESRI.ArcGIS.Geodatabase.FieldClass();
 7             //todo(IFieldEdit2).
 8             //面ID.
 9             ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldIdEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldId;
10             fieldIdEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
11             fieldIdEdit.Name_2 = "面ID";
12
13             //点数.
14             ESRI.ArcGIS.Geodatabase.IField fieldCount = new ESRI.ArcGIS.Geodatabase.FieldClass();
15             ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldCountEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldCount;
16             fieldCountEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
17             fieldCountEdit.Name_2 = "个数";
18
19             ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();
20             ESRI.ArcGIS.Geodatabase.IFields fields = objClsDes.RequiredFields;
21             ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields;
22             fieldsEdit.AddField(fieldId);
23             fieldsEdit.AddField(fieldCount);
24             return fields;
25         }

  2,根据Fields穿件 ITable

 1 /// <summary>
 2         /// Creates a table with some default fields.
 3         /// </summary>
 4         /// <param name="workspace">An IWorkspace2 interface</param>
 5         /// <param name="tableName">表名称,如: "owners"</param>
 6         /// <param name="fields">An IFields interface or Nothing.</param>
 7         /// <returns></returns>
 8         public static ESRI.ArcGIS.Geodatabase.ITable CreateTable(ESRI.ArcGIS.Geodatabase.IWorkspace2 workspace, System.String tableName, ESRI.ArcGIS.Geodatabase.IFields fields) {
 9             // create the behavior clasid for the featureclass
10             ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
11             // valid feature workspace not passed in as an argument to the method
12             if (workspace == null) return null;
13
14             ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
15
16             // table with that name already exists return that table
17             if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable, tableName))
18                 return featureWorkspace.OpenTable(tableName);
19
20             uid.Value = "esriGeoDatabase.Object";
21
22             ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();
23
24             // if a fields collection is not passed in then supply our own
25             if (fields == null) {
26                 // create the fields using the required fields method
27                 fields = objClsDes.RequiredFields;
28                 ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast
29
30                 ESRI.ArcGIS.Geodatabase.IField field = new ESRI.ArcGIS.Geodatabase.FieldClass();
31
32                 // create a user defined text field
33                 ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit)field;
34
35                 // setup field properties
36                 fieldEdit.Name_2 = "SampleField";
37                 fieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
38                 fieldEdit.IsNullable_2 = true;
39                 fieldEdit.AliasName_2 = "Sample Field Column";
40                 fieldEdit.DefaultValue_2 = "test";
41                 fieldEdit.Editable_2 = true;
42                 fieldEdit.Length_2 = 100;
43
44                 // add field to field collection
45                 fieldsEdit.AddField(field);
46                 fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit;
47             }
48
49             // Use IFieldChecker to create a validated fields collection.
50             ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
51             ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null;
52             ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;
53             fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
54             fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
55
56             // The enumFieldError enumerator can be inspected at this point to determine
57             // which fields were modified during validation.
58
59             // create and return the table
60             return featureWorkspace.CreateTable(tableName, validatedFields, uid, null, "");
61         }

  3,根据创建ITable的结构,统计"面"要素中"点"要素的个数

 1 /// <summary>
 2         /// 查询"面"要素中的"点个数".
 3         /// </summary>
 4         /// <param name="polygonFeatureClass">"面"要素类.</param>
 5         /// <param name="pointFeatureClass">"点"要素类.</param>
 6         /// <param name="t">ITable表.</param>
 7         /// <returns></returns>
 8         public static ESRI.ArcGIS.Geodatabase.ITable PointInPolygonCount(ESRI.ArcGIS.Geodatabase.IFeatureClass polygonFeatureClass, ESRI.ArcGIS.Geodatabase.IFeatureClass pointFeatureClass, ESRI.ArcGIS.Geodatabase.ITable t) {
 9             if (!(polygonFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon))
10                 throw new InvalidCastException("Statistics_Assist::PointInPolygonCount:polygonFeatureClass");
11             if (!(pointFeatureClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint))
12                 throw new InvalidCastException("Statistics_Assist::PointInPolygonCount:pointFeatureClass");
13
14             ESRI.ArcGIS.Geodatabase.ISpatialFilter polySpatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
15             polySpatialFilter.WhereClause = "name like ‘%辽宁%‘"; //指定名称的"面"要素.
16
17             ESRI.ArcGIS.Geodatabase.IFeatureCursor fCurPoly = polygonFeatureClass.Search(polySpatialFilter, false);
18             ESRI.ArcGIS.Geodatabase.IFeature fPoly = null;
19             while ((fPoly = fCurPoly.NextFeature()) != null) {
20                 ESRI.ArcGIS.Geometry.IGeometry polyGeo = fPoly.Shape;
21                 ESRI.ArcGIS.Geodatabase.ISpatialFilter pntSpatialFilter = new ESRI.ArcGIS.Geodatabase.SpatialFilterClass();
22                 pntSpatialFilter.Geometry = polyGeo;
23                 //包含在"面"要素中的"点".
24                 pntSpatialFilter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains;
25                 ESRI.ArcGIS.Geodatabase.IFeatureCursor fCurPnt = pointFeatureClass.Search(pntSpatialFilter, false);
26                 ESRI.ArcGIS.Geodatabase.IFeature fPnt = null;
27                 int count = 0;  ////"点"要素的个数.
28                 while ((fPnt = fCurPnt.NextFeature()) != null)
29                     ++count;
30
31                 if (count != 0) {
32                     ESRI.ArcGIS.Geodatabase.IRow r = t.CreateRow();
33                     r.set_Value(1, fPoly.get_Value(0)); //注意的是,0下标是不可更改的(OID).
34                     r.set_Value(2, count);
35                     r.Store();
36                 }
37             }
38
39             return t;
40         }

  4,在住函数中调用

 1 try {
 2                 //面.
 3                 ESRI.ArcGIS.Carto.IFeatureLayer PolyFLyr = (ESRI.ArcGIS.Carto.IFeatureLayer)Engine.App_Code.Layer_Assist.GetLayerByName(mapCtrl_main.Map, "省市");
 4
 5                 //点.
 6                 ESRI.ArcGIS.Carto.IFeatureLayer PntFLyr = (ESRI.ArcGIS.Carto.IFeatureLayer)Engine.App_Code.Layer_Assist.GetLayerByName(mapCtrl_main.Map, "地市级以上居民地");
 7
 8                 //表.
 9                 string tPath = @"G:\doc\gis\1.400\data\feature";
10
11                 //1,根据"表路径"和"表名"创建.
12                 //ESRI.ArcGIS.Geodatabase.ITable tOri = Engine.App_Code.AttributeTable_Assist.CreateTable(tPath, "tOri");
13                 //2,根据"表路径"和"表名",以及"字段"创建.
14                 ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
15                 ESRI.ArcGIS.Geodatabase.IWorkspace2 ws = (ESRI.ArcGIS.Geodatabase.IWorkspace2)wsf.OpenFromFile(tPath, 0);
16                 ESRI.ArcGIS.Geodatabase.IFields fields = Engine.App_Code.AttributeTable_Assist.CreateFields();
17                 ESRI.ArcGIS.Geodatabase.ITable t = Engine.App_Code.AttributeTable_Assist.CreateTable(ws, "fields_table_LiaoNing", fields);
18                 //查询面中的点.
19                 ESRI.ArcGIS.Geodatabase.ITable tRes = Engine.App_Code.Statistics_Assist.PointInPolygonCount(PolyFLyr.FeatureClass, PntFLyr.FeatureClass, t);
20             }

可以通过"表路径"和"表名"创建ITable

 1 /// <summary>
 2         /// 创建表.
 3         /// </summary>
 4         /// <param name="tablePath">即将创建表的路径,如:"G:\doc\gis\1.400\data\feature"</param>
 5         /// <param name="tableName">表名,如:"owner"</param>
 6         /// <returns></returns>
 7         public static ESRI.ArcGIS.Geodatabase.ITable CreateTable(string tablePath, string tableName) {
 8             //需要注意的是必须为"IWorkspaceFactory2",不能为"IWorkspaceFactory".
 9             ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
10             ESRI.ArcGIS.Geodatabase.IFeatureWorkspace fws = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)wsf.OpenFromFile(tablePath, 0);
11
12             ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();
13
14             ESRI.ArcGIS.Geodatabase.IField fieldId = new ESRI.ArcGIS.Geodatabase.FieldClass();
15             //todo(IFieldEdit2).
16             //面ID.
17             ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldIdEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldId;
18             fieldIdEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
19             fieldIdEdit.Name_2 = "面ID";
20
21             //点数.
22             ESRI.ArcGIS.Geodatabase.IField fieldCount = new ESRI.ArcGIS.Geodatabase.FieldClass();
23             ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldCountEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit2)fieldCount;
24             fieldCountEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger;
25             fieldCountEdit.Name_2 = "个数";
26
27             ESRI.ArcGIS.Geodatabase.IObjectClassDescription objClsDes = new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();
28             ESRI.ArcGIS.Geodatabase.IFields fields = objClsDes.RequiredFields;
29             ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields;
30             fieldsEdit.AddField(fieldId);
31             fieldsEdit.AddField(fieldCount);
32
33             return fws.CreateTable(tableName, fields, uid, null, "");
34         }
时间: 2024-07-29 08:47:06

统计"面"要素中"点"要素的个数.的相关文章

统计一个字符串中的单词的个数,并打印各个单词

/*测试数据:Shen zhen is a beautiful city!*/ /*运行结果:Word:6 Shen zhen is a beautiful city!*/ #include<stdio.h> #define SIZE 1000 void wordCount(char *str) { int count = 0, flag = 0; char *p = str; while (*p != '\0'){ while (*p == 32){ if (*(p + 1) == 0){/

统计js数组中奇数元素的个数

如何统计一个JS数组中奇数元素的个数呢? 这是群友提出的一个问题,大部分群友给出的是遍历 然后对2取模,得到最终结果. 这样的写法是最容易想得到的,那么有没有其他思路呢? 这里我提供另外一种思路,我们知道奇数其实就是以 1 3 5 7 9 作为末尾结尾的数字,那么只要统计这些数字出现的次数就够了,但是光这样统计容易误算,所以我们可以先用逗号拼接起来,连着逗号一起计算,由于js没有php那么方便的能用substr_count 函数统计字符串出现次数,所以我们直接采用正则替换,计算长度差得到个数,代

统计二进制展开中数位1的个数的优化

问题: 对于任意的非负整数,统计其二进制展开中数位1的总数. 解决: 相关Blog:http://www.cnblogs.com/maples7/p/4324844.html 在看这篇之前可以先看看上述这篇,这篇主要讨论其优化问题. 常规解法: O(logn): 1 int countOnes(unsigned int n) 2 { 3 int ones = 0; 4 while (0 < n) 5 { 6 ones += (1 & n); 7 n >>= 1; 8 } 9 re

统计一行字符中各类字符的个数

/*输出一行字符,分类统计字符个数*/ #include<stdio.h>#include<stdlib.h>int main(void){ int letter=0,space=0,digit=0,other=0;/*定义变量并初始化*/ char c;/*定义字符串c*/ printf("请输入一行字符,以回车键结束\n"); while((c=getchar())!='\n')/*判断c是否是回车键*/ if(c>='a'&&c<

统计numpy数组中每个值的个数

import numpy as np from collections import Counter data = np.array([1.1,2,3,4,4,5]) Counter(data) #简单方法 sum(data==4) 原文地址:https://www.cnblogs.com/wzdLY/p/9630717.html

JS实现如何的统计一个字符串中相同的字符个数

  1 var string="start,stop,speed,start,speed,start,relocicty,start,start,start,start"; 2 var count=1; 3 for (let index = 0; index < string.length; index++) { 4 var a=string.indexOf("start",index); 5 // console.log(a); 6 if(a!=-1&

UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)

题目链接:http://acm.upc.edu.cn/problem.php?id=2224 题意:给出n个数pi,和m个查询,每个查询给出l,r,a,b,让你求在区间l~r之间的pi的个数(A<=pi<=B,l<=i<=r). 参考链接:http://www.cnblogs.com/zj62/p/3558967.html #include <iostream> #include <cstdio> #include <cstring> #incl

【c语言】统计一个数二进制中的1的个数

// 统计一个数二进制中的1的个数 #include <stdio.h> int count(int a) { int count = 0; while (a) { count++; a = a & (a - 1); } return count; } int main() { printf("%d\n", count(10)); printf("%d\n", count(0)); printf("%d\n", count(-

字符串之“统计一个字符串中单词的个数”

题目:统计一个字符串中单词的个数 输入一行字符,统计其中有多少个单词,单词之间用空格分隔开 输入:my name is jacky 输出:the number of word is 4 代码如下: #include <stdio.h> int main(int argc, char *argv[]) { char str[80]; int i=0,num=0,flag=0; char c; gets(str); while((c=str[i])!='\0') { if(c==' ') flag