using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Configuration;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
namespace sandian
{
public partial class SaDian : Form
{
public XmlDocument xmldoc = new XmlDocument();
public static XmlNodeList nodelist;
public string[] headText = new string[10] { "路段名","起始道路","终止道路", "所处方向", "点X坐标","点Y坐标","序号","统计路段名","统计总计","统计重复点" };
List<LuDuan> LuDuanList=new List<LuDuan>();
public System.Reflection.Missing miss;
public static Microsoft.Office.Interop.Excel.ApplicationClass excel;
public static Microsoft.Office.Interop.Excel.Workbooks books;
public static Microsoft.Office.Interop.Excel.Workbook book;
public static Microsoft.Office.Interop.Excel.Worksheet sheet;
public static int ExcelX = 2;
public static int ExcelY = 1;
public static int filterDistance =30;
public SaDian()
{
InitializeComponent();
init();
}
public void initExcel()
{
miss = System.Reflection.Missing.Value;
excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
//若是true,则在导出的时候会显示EXcel界面。
excel.Visible = true;
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "test";
//生成字段名称,逐条写,无效率
for (int i = 0; i < headText.Length; i++)
{
excel.Cells[1, i + 1] = headText[i];
}
}
public void init()
{
xmldoc.Load("setup.xml");
nodelist = xmldoc.SelectSingleNode("setup").ChildNodes;//第一个节点
// initExcel();
}
private void button1_Click(object sender, EventArgs e)
{
// string MxdPath = OpenMxd();
// axMapControl1.LoadMxFile(MxdPath);
// axMapControl1.AddShapeFile();
string[] ShpFile=OpenShapeFile();
string FilePath = ShpFile[0];
string ShpName = ShpFile[1];
axMapControl1.AddShapeFile(FilePath, ShpName);
}
public string OpenMxd()
{
string MxdPath = "";
OpenFileDialog OpenMXD = new OpenFileDialog();
OpenMXD.Title = "打开地图";
OpenMXD.InitialDirectory = "E:";
OpenMXD.Filter = "Map Documents (*.mxd)|*.mxd";
if (OpenMXD.ShowDialog() == DialogResult.OK)
{
MxdPath = OpenMXD.FileName;
}
return MxdPath;
}
//选择shape文件
public string[] OpenShapeFile()
{
string[] ShpFile = new string[2];
OpenFileDialog OpenShpFile = new OpenFileDialog();
OpenShpFile.Title = "打开Shape文件";
OpenShpFile.InitialDirectory = "E:";
OpenShpFile.Filter = "Shape文件(*.shp)|*.shp";
if (OpenShpFile.ShowDialog() == DialogResult.OK)
{
string ShapPath = OpenShpFile.FileName;
//利用"\\"将文件路径分成两部分
int Position = ShapPath.LastIndexOf("\\");
string FilePath = ShapPath.Substring(0, Position);
string ShpName = ShapPath.Substring(Position + 1);
ShpFile[0] = FilePath;
ShpFile[1] = ShpName;
}
return ShpFile;
}
//读取shape文件,新建图层,并显示在地图
private void button2_Click(object sender, EventArgs e)
{
// IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); // 1
// IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); // 1
string[] ShpFile = OpenShapeFile();
string FilePath = ShpFile[0];
string ShpName = ShpFile[1];
MessageBox.Show(FilePath);
MessageBox.Show(ShpName);
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(FilePath, 0); // 2
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(ShpName); //3
IFeatureLayer pFLayer = new FeatureLayerClass(); // 4
pFLayer.FeatureClass = pFC;
pFLayer.Name = pFC.AliasName; // 5
ILayer pLayer = pFLayer as ILayer;
IMap pMap = axMapControl1.Map;
pMap.AddLayer(pLayer); // 6
axMapControl1.ActiveView.Refresh();
}
// 计算两点之间的距离
private static double lineSpace(double x1, double y1,double x2, double y2) {
double lineLength = 0;
lineLength = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));
return lineLength;
}
public static string FangXiang(double x1, double y1, double x2, double y2, double x0, double y0)
{
double s = 0;
s = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0);
if (s > 0)
{
return "逆时针";
}
if (s == 0)
{
return "在一条线上";
}
if (s < 0)
{
return "顺时针";
}
return "未处理结果";
}
//点到线段距离
private static double pointToLine(double x1, double y1, double x2, double y2, double x0, double y0)
{
double space = 0;
double a, b, c;
a = lineSpace(x1, y1, x2, y2);// 线段的长度
b = lineSpace(x1, y1, x0, y0);// (x1,y1)到点的距离
c = lineSpace(x2, y2, x0, y0);// (x2,y2)到点的距离
if (c <= 0.000001 || b <= 0.000001) {
space = 1000000;
return 1000000;
}
if (a <= 0.000001) {
space = 1000000;
return 1000000;
}
if (c * c >= a * a + b * b) {
space = 1000000;
return 1000000;
}
if (b * b >= a * a + c * c) {
space = 1000000;
return 1000000;
}
double p = (a + b + c) / 2;// 半周长
double s = Math.Sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)
return space;
}
public static int doQueryShape(string startName,string endName,string lutemp,IPolyline pLine,IGeometry bufferGeometry)
{
string luduanName = lutemp;
double d = pLine.Length;
IPoint spPoint = new PointClass();
pLine.QueryPoint(esriSegmentExtension.esriNoExtension, 0, false, spPoint);
IPoint mpPoint = new PointClass();
pLine.QueryPoint(esriSegmentExtension.esriNoExtension,d,false,mpPoint);
//pLine.QueryPoint(esriSegmentExtension.esriNoExtension,0,false,
string FilePath = @"D:\撒点";
string ShpName="全市站牌.shp";
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); // 1
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(FilePath, 0); // 2
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(ShpName); //3
IFeatureLayer pFLayer = new FeatureLayerClass(); // 4
pFLayer.FeatureClass = pFC;
//IQueryFilter qf = new QueryFilterClass();
//qf.WhereClause = "";
pFLayer.SpatialReference = bufferGeometry.SpatialReference;
// IGeometry geo = bufferGeometry.Envelope;
// (geo as IEnvelope).Expand(1000, 1000, false);
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
pSpatialFilter.Geometry = bufferGeometry;
// IFeatureSelection pFeatureSelection = pFLayer as IFeatureSelection;
// pFeatureSelection.SelectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
//QI到ISelectionSet
// ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet;
// int i = 0;
// i = pSelectionSet.Count;
// return i;
IFeatureCursor featureCursor = pFLayer.Search(pSpatialFilter, false);
//IFeatureCursor featureCursor = pFLayer.Search(qf, false);
ESRI.ArcGIS.Geodatabase.IFeature pFeature = featureCursor.NextFeature();
int i = 0;
int p = 0;
while (pFeature != null)
{
Multipoint mp = pFeature.Shape as Multipoint;
for (int g = 0; g < mp.PointCount; g++)
{
IPoint point1 = mp.get_Point(g);
double distance=pointToLine(spPoint.X, spPoint.Y, mpPoint.X,mpPoint.Y,point1.X,point1.Y);
string direct = FangXiang(spPoint.X, spPoint.Y, mpPoint.X, mpPoint.Y, point1.X, point1.Y);
if (distance < 30)
{
Boolean MrRight = true;
for (int k = g + 1; k < mp.PointCount; k++)
{
if(lineSpace(point1.X,point1.Y,mp.get_Point(k).X,mp.get_Point(k).Y)<filterDistance)
{
MrRight = false;
p++;
}
}
if (MrRight == true)
{
i++;
// Console.WriteLine(luduanName + ": 符合的" + direct + "点坐标:" + "X=" + point1.X + " Y=" + point1.Y + " 所处位置" + i.ToString());
excel.Cells[ExcelX, 1] = luduanName;
excel.Cells[ExcelX, 2] = startName;
excel.Cells[ExcelX, 3] = endName;
excel.Cells[ExcelX, 4] = direct;
excel.Cells[ExcelX, 5] = point1.X.ToString();
excel.Cells[ExcelX, 6] = point1.Y.ToString();
excel.Cells[ExcelX, 7] = i.ToString();
ExcelX += 1;
}
}
}
pFeature = featureCursor.NextFeature();
}
if (i > 0)
{
// excel.get_Range(excel.Cells[ExcelX, 8], excel.Cells[ExcelX, 10]).Font.Color = ColorTranslator.ToOle(Color.);
excel.Cells[ExcelX, 8] = luduanName;
excel.Cells[ExcelX, 9] ="总计:"+ i.ToString();
excel.Cells[ExcelX, 10] ="重复点:"+ p.ToString();
excel.get_Range(excel.Cells[ExcelX, 8], excel.Cells[ExcelX, 10]).Interior.ColorIndex = 6;
ExcelX += 1;
}
return i;
}
public static string Get_Xml(string xml_name)
{
string rusult = nodelist.Item(0).SelectSingleNode(xml_name).InnerText;
return rusult;
}
private void button3_Click(object sender, EventArgs e)
{
/*
* 获取保存EXCEL的路径
*/
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
//strName储存保存EXCEL路径
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
initExcel();
string sdeServer = Get_Xml("SDEServer");
string sdeInstance = Get_Xml("SDEInstance");
string sdeDatabase = Get_Xml("SDEDatabase");
string sdeUser = Get_Xml("SDEUser");
string sdePassword = Get_Xml("SDEPwd");
string sdeVersion = Get_Xml("SDEVersion");
double bufferDis = Convert.ToDouble(Get_Xml("bufferDistance"));
double filterDis = Convert.ToDouble(Get_Xml("filterDistance"));
IWorkspaceFactory sdeWorkspaceFactory;
IFeatureWorkspace workSpace;
try
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
//授权信息加载
ESRI.ArcGIS.esriSystem.IAoInitialize m_AoInitialize = new ESRI.ArcGIS.esriSystem.AoInitialize();
m_AoInitialize.Initialize(ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeArcInfo);
//进行license控件的初始化
IAoInitialize m_pAoInit = new AoInitializeClass();
m_pAoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcServer);
IPropertySet propSet = new PropertySetClass();
propSet.SetProperty("SERVER", sdeServer);
propSet.SetProperty("INSTANCE", sdeInstance);
propSet.SetProperty("USER", sdeUser);
propSet.SetProperty("PASSWORD", sdePassword);
propSet.SetProperty("DATABASE", sdeDatabase);
propSet.SetProperty("VERSION", sdeVersion);
sdeWorkspaceFactory = new SdeWorkspaceFactoryClass();
workSpace = (IFeatureWorkspace)sdeWorkspaceFactory.Open(propSet, 0);
// MessageBox.Show("数据库练级成功!");
//得到路段图层
IFeatureClass pFCLuDuan = (workSpace as IFeatureWorkspace).OpenFeatureClass("路段");
IFeatureLayer pFLLuDuan = new FeatureLayer();
pFLLuDuan.FeatureClass = pFCLuDuan;
pFLLuDuan.Name = "路段";
//创建路段图层过滤器
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = "所属区县=" + "'浦东新区'";
//获得路段图层游标
IFeatureCursor pFeatureCursor = pFLLuDuan.FeatureClass.Search(pQueryFilter, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
IPolyline pLine = pFeature.Shape as IPolyline;
IGeometry bufferGeometry = bufferPolyLine(pLine, bufferDis);
object luduanName = pFeature.get_Value(pFeature.Fields.FindField("路段名称"));
string luTemp = luduanName.ToString();
// MessageBox.Show("缓冲了!");
string startName = pFeature.get_Value(pFeature.Fields.FindField("起始道路")).ToString();
string endName = pFeature.get_Value(pFeature.Fields.FindField("终止道路")).ToString();
//targetArr[ax, 0] = startName;
//targetArr[ax, 1] = endName;
//targetArr[ax, 2] = luTemp;
//targetArr[ax, 3] = pLine;
//targetArr[ax, 4] = bufferGeometry;
//ax += 1;
LuDuanList.Add(new LuDuan(){ _startName = startName, _endName = endName, _luTemp = luTemp, _pLine = pLine, _bufferGeometry = bufferGeometry });
// int temp = doQueryShape(startName,endName,luTemp, pLine, bufferGeometry);
// MessageBox.Show(temp.ToString());
// Console.WriteLine(luduanName.ToString()+" "+temp.ToString());
pFeature = pFeatureCursor.NextFeature();
}
// string strName = @"D:\撒点\撒点统计.xls";
//Console.WriteLine(targetArr[0,0].ToString());
//for (int kk = 0; kk < targetArr.Length; kk++)
//{
// string p1 = targetArr[kk, 0].ToString();
// string p2 = targetArr[kk,1].ToString();
// string p3 = targetArr[kk, 2].ToString();
// IPolyline p4 = targetArr[kk, 3] as IPolyline;
// IGeometry p5 = targetArr[kk, 4] as IGeometry;
// doQueryShape(p1,p2,p3,p4,p5);
//}
LuDuanList.Sort(new SortName());
foreach (LuDuan item in LuDuanList)
{
// Console.WriteLine(item._luTemp);
doQueryShape(item._startName, item._endName, item._luTemp,item._pLine,item._bufferGeometry);
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
ILayer pLayer = pFLLuDuan as ILayer;
IMap pMap = axMapControl1.Map;
pMap.AddLayer(pLayer);
axMapControl1.ActiveView.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
public IGeometry bufferPolyLine(IPolyline pPolyline, double dis)
{
ITopologicalOperator topolOperator = pPolyline as ITopologicalOperator;
IGeometry bufferGeometry = topolOperator.Buffer(dis);
return bufferGeometry;
}
private void button4_Click(object sender, EventArgs e)
{
//进行license控件的初始化
// IAoInitialize m_pAoInit = new AoInitializeClass();
// m_pAoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcServer);
string FilePath = @"D:\sd";
string ShpName = "全市站牌.shp";
IWorkspaceFactory pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory(); // 1
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(FilePath, 0); // 2
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(ShpName); //3
IFeatureLayer pFLayer = new FeatureLayerClass(); // 4
pFLayer.FeatureClass = pFC;
pFLayer.Name = pFC.AliasName; // 5
ILayer pLayer = pFLayer as ILayer;
IMap pMap = axMapControl1.Map;
pMap.AddLayer(pLayer); // 6
axMapControl1.ActiveView.Refresh();
}
}
}
class LuDuan : IComparable<LuDuan>
{
public string _startName { get; set; }
public string _endName { get; set; }
public string _luTemp { get; set; }
public IPolyline _pLine {get;set;}
public IGeometry _bufferGeometry {get;set;}
#region IComparable<Student> Members
public int CompareTo(LuDuan other)
{
return _luTemp.CompareTo(other._luTemp);
}
#endregion
}
class SortName : IComparer<LuDuan>
{
#region IComparer<LuDuan> Members
public int Compare(LuDuan x, LuDuan y)
{
return x._luTemp.CompareTo(y._luTemp);
}
#endregion
}
分享到:
相关推荐
一般使用springjdbc、hibernate的sql查询,库获取到的数据都是List<Map<String, Object>>结果集,如果我们要转化为JavaBean,则需要做一系列的map.get(),然后obj.set()。 此工程中就是解决List<Map<String, Object>...
本篇文章将深入探讨三种常见的集合类型:Array、ArrayList、Hashtable以及泛型的List<T>,并提供相关的示例代码来帮助理解它们的用法。 ### 1. Array(数组) 数组是最基础的集合类型,它允许存储相同类型的元素...
List<T>的基本操作包括声明、添加元素、遍历、删除元素、排序和反转等。 1. 声明: - List<T> myList = new List<T>(); 例如,List<string> myList = new List<string>(); - List<T> testList = new List<T>...
List<map>,List<Map<String, Object>>,多字段组合排序。提供一个简易的思路,如果需要进行参考。
List<Record> list = new ArrayList<Record>(); // 添加记录到列表中 Collections.sort(list); ``` 方法二:使用Comparator接口和Collator类 在Java中,我们还可以使用Comparator接口和Collator类来实现自定义排序...
为了解决`BindingList<T>`不支持自动排序的问题,我们可以创建一个自定义的`SortableBindingList<T>`类,继承自`BindingList<T>`,并在其中重写相关的成员方法以支持排序功能。 ```csharp public class ...
List<Student> list = new ArrayList<>(); // 添加元素到list Set<Student> set = new HashSet<>(list); list.clear(); list.addAll(set); ``` 3. 使用Java 8的Stream API去重 Java 8中引入了Stream API,可以使用...
假设我们有一个`LocalDate`对象的列表`List<LocalDate> dates`,可以使用以下步骤进行排序: ```java List<LocalDate> sortedDates = dates.stream() .sorted(Comparator.naturalOrder()) .collect(Collectors...
Map<Integer, String> mapOfEmployees = employees.stream().collect( Collectors.toMap(e -> e.getEmpId(), e -> e.getEmpName())); 在上面的代码中,我们使用了Collectors.toMap()方法来将List转换为Map,key是...
List<T> dataList = new ArrayList<>(); while (rs.next()) { T data = ...; // 反射或者使用ORM框架如MyBatis获取数据对象 dataList.add(data); } // 计算总记录数 Statement countStmt = conn....
= typeof(List<>)) throw new NotSupportedException("数据源不支持排序"); // 获取泛型参数类型 var elementType = listType.GetGenericArguments()[0]; // 创建比较器 var comparer = Comparer[elementType...
List<Student> deserializedStudents = JsonConvert.DeserializeObject<List<Student>>(jsonString); ``` 这里,`JsonConvert.SerializeObject`方法用于序列化对象到JSON,而`JsonConvert.DeserializeObject`则用于...
`DynamicOrderBy`函数接受一个`List<T>`和一个字段名数组,然后通过`BuildOrderByExpression`构建一个表示排序规则的Lambda表达式。`BuildOrderByExpression`使用`Expression.Property`来逐个获取指定的字段,并通过...
Java中对List<Map>根据Map某个key值进行排序的方法 在 Java 中,排序是一个非常常见的操作,特别是在处理 List 集合时。当我们需要根据 Map 中的某个 key 值对 List 集合进行排序时,需要使用Comparator接口来实现...
- Equals(Object):比较当前 List<T> 是否与指定对象相等。 - Find(Predicate<T>):查找符合指定条件的第一个元素。 - FindAll(Predicate<T>):查找符合指定条件的所有元素。 - FindIndex(Predicate<T>):返回...
22.zip<br>Dragging Items to Rearrange Rows<br>重新排列行数(5KB)<END><br>12,24.zip<br>Allowing items to be edited<br>允许列表项编辑(2KB)<END><br>13,27.zip<br>Using a drop down list to change a subitem...
24<br><br>0051 变量的作用域 25<br><br>2.5 其他 26<br><br>0052 有效使用this对象 26<br><br>0053 如何声明变量 26<br><br>0054 如何声明相同类型的多个变量 26<br><br>0055 利用Object变量传递参数 ...
List<Map<String,Object>> listMap1 = new LinkedList<Map<String,Object>>(); // ... Set<Map> setMap = new HashSet<Map>(); for(Map<String,Object> map1 : listMap1){ if(setMap.add(map1)){ listMap2.add(map...
protected static List<object> MinGroup(string columnName, string condition, string groupBy); protected static DataTable Query(PagingArg pagingArg, params string[] propertyNames); protected static ...
List<T>类是ArrayList类的泛型等效类。该类使用大小可按需动态增加的数组实现IList<T>泛型接口。泛型的好处是不会强行对值类型进行装箱和拆箱,或对引用类型进行向下强制类型转换,从而提高性能。 1. List的简介 ...