public string CreateShpByROI() { string empty = string.Empty; IGeometryCollection geometryCollection = new PolygonClass(); object missing = Type.Missing; foreach (KeyValuePair <int, ROILayerClass> current in FrmROI.RoiLayerDic) { foreach (ROIElementClass current2 in current.Value.ElementList) { if (current2.Checked) { geometryCollection.AddGeometryCollection(current2.Element.Geometry as IGeometryCollection); } } } if (geometryCollection.GeometryCount > 0) { IGeometry geometry = geometryCollection as IGeometry; geometry.SpatialReference = EnviVars.instance.MapControl.SpatialReference; if (geometry != null) { if (!ROIManager.instance.GpExecutor.CreateShpFile(geometry, out empty)) { empty = string.Empty; } } } return(empty); }
// //Create vector file by ROI // public string CreateShpByROI(List <string> classList) { string empty = string.Empty; List <ROIGeometry> roiGeometryList = new List <ROIGeometry>(); object missing = Type.Missing; foreach (KeyValuePair <int, ROILayerClass> current in roiLayerDic) { foreach (string name in classList) { if (current.Value.Name == name) { IGeometryCollection geometryCollection = new PolygonClass(); foreach (ROIElementClass current2 in current.Value.ElementList) { if (current2.Checked) { geometryCollection.AddGeometryCollection(current2.Element.Geometry as IGeometryCollection); } } if (geometryCollection.GeometryCount > 0) { IGeometry geometry = geometryCollection as IGeometry; geometry.SpatialReference = spatialReference; if (geometry != null) { roiGeometryList.Add(new ROIGeometry(name, geometry)); //if (!ROIManager.instance.GpExecutor.CreateShpFile(geometry, out empty)) //{ // empty = string.Empty; //} } } } } } if (!this.CreateShpFile(roiGeometryList, out empty)) { empty = string.Empty; } return(empty); }
//将多边形添加 public static IPolygon MergePolygons(IPolygon plygon1, IPolygon plygon2) { try { IGeometryCollection pGCollection1 = new PolygonClass(); IGeometryCollection pGCollection2 = plygon1 as IGeometryCollection; IGeometryCollection pGCollection3 = plygon2 as IGeometryCollection; //添加多边形 pGCollection1.AddGeometryCollection(pGCollection2); pGCollection1.AddGeometryCollection(pGCollection3); ITopologicalOperator pTopological = pGCollection1 as ITopologicalOperator; pTopological.Simplify(); IPolygon pPolygon = pGCollection1 as IPolygon; return pPolygon; } catch(Exception Err) { return null; } }
public override bool Work() { base.Work(); if (!Init()) { Finish(); return(false); } using (var reader = new StreamReader(_txtFilePath, Encoding.GetEncoding("GB2312"))) { var line = reader.ReadLine(); var tokens = new string[] { "", "", "", "", "", "" }; var row = 1; var polygon = new PolygonClass(); var ring = new RingClass(); var pc = ring as IPointCollection; var first = string.Empty; IGeometryCollection resultPolygon = new PolygonClass(); while (line != null && !string.IsNullOrEmpty(line)) { line = line.Trim(); tokens = line.Split(','); if (tokens.Length != 6) { _errors.Add(string.Format("文件第{0}行格式不正确", row)); line = reader.ReadLine(); row++; continue; } IPoint pt = new PointClass(); double x, y; if ((!double.TryParse(tokens[4], out y) || !double.TryParse(tokens[5], out x)) || x < 40000000 || x > 41000000 || y < 2000000 || y > 4000000) { _errors.Add(string.Format("文件第{0}行的格式有误,坐标应为数字类型", row)); polygon = new PolygonClass(); pc = polygon as IPointCollection; break; } pt.X = x; pt.Y = y; pc.AddPoint(pt, ref missing, ref missing); var temp = string.Format("{0},{1}", tokens[4], tokens[5]); if (string.IsNullOrEmpty(first)) { first = temp; } else { if (first == temp) { if (pc.PointCount > 0) { if (pc.PointCount < 4) { _errors.Add(string.Format("文件第{0}行之前的坐标串有误,多边形顶点数不应少于4个", row)); ring = new RingClass(); pc = ring as IPointCollection; break; } else { polygon.AddGeometry(ring, ref missing, ref missing); } } if (polygon.GeometryCount > 0) { polygon.ITopologicalOperator2_IsKnownSimple_2 = false; (polygon as ITopologicalOperator2).Simplify(); polygon.GeometriesChanged(); resultPolygon.AddGeometryCollection(polygon as IGeometryCollection); } polygon = new PolygonClass(); ring = new RingClass(); pc = polygon as IPointCollection; first = string.Empty; } } line = reader.ReadLine(); row++; } if (pc.PointCount > 0) { if (pc.PointCount < 4) { _errors.Add(string.Format("文件第{0}行之前的坐标串有误,多边形顶点不应少于4个", row)); } else { polygon.AddGeometry(ring, ref missing, ref missing); } if (polygon.GeometryCount > 0) { polygon.ITopologicalOperator2_IsKnownSimple_2 = false; (polygon as ITopologicalOperator2).Simplify(); polygon.GeometriesChanged(); resultPolygon.AddGeometryCollection(polygon as IGeometryCollection); } } if (resultPolygon.GeometryCount > 0) { try { var buffer = _featureClass.CreateFeatureBuffer(); ITopologicalOperator top = resultPolygon as ITopologicalOperator; top.Simplify(); buffer.Shape = resultPolygon as IPolygon; _featureCursor.InsertFeature(buffer); }catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); } } } Finish(); return(true); }
public override bool Work() { base.Work(); if (!Init()) { Finish(); return(false); } var current = 1; foreach (var item in _txtFiles) { StepProgressor.Message = string.Format("{0}/{1}——正在分析读取文件:{2}", current++, _txtFiles.Count, System.IO.Path.GetFileNameWithoutExtension(item)); CanContinue = TrackCancel.Continue(); if (!CanContinue) { break; } //#region 方法二 //using (var reader = new StreamReader(item, Encoding.GetEncoding("GB2312"))) //{ // var line = reader.ReadLine(); // var begin = false; // var polygon = new PolygonClass(); // var ring = new RingClass(); // var pc = ring as IPointCollection; // var ringId = string.Empty; // var lastTokens = new[] { "", "", "", "", "", "", "", "", "" }; // var row = 1; // var pgCount = 0; // var ext = false; // var list = new List<IPoint>(); // while (line != null) // { // line = line.Trim(); // if (line == "[地块坐标]") // { // begin = true; // line = reader.ReadLine(); // row++; // ext = true; // continue; // } // if (begin && !string.IsNullOrEmpty(line)) // { // string[] tokens = line.Split(','); // if (tokens[tokens.Length - 1] == "@") // { // #region // if (tokens.Length != 9) // { // _errors.Add(string.Format("文件第{0}行的格式有误,字段数量应为9", row)); // break; // } // if (pc.PointCount > 0) // { // if (pc.PointCount < 4) // { // _errors.Add(string.Format("文件第{0}行之前的坐标串有误,多边形顶点数不应少于4个", row)); // ring = new RingClass(); // pc = ring as IPointCollection; // break; // } // else // { // polygon.AddGeometry(ring, ref missing, ref missing); // } // } // //if (polygon.GeometryCount > 0) // //{ // // var buffer = FeatureClass.CreateFeatureBuffer(); // // polygon.ITopologicalOperator2_IsKnownSimple_2 = false; // // (polygon as ITopologicalOperator2).Simplify(); // // polygon.GeometriesChanged(); // // buffer.Shape = polygon; // // buffer.set_Value(buffer.Fields.FindField("FILENAME"), System.IO.Path.GetFileName(item)); // // buffer.set_Value(buffer.Fields.FindField("DKBH"), lastTokens[2]); // // buffer.set_Value(buffer.Fields.FindField("DKMC"), lastTokens[3]); // // buffer.set_Value(buffer.Fields.FindField("JLTXSX"), lastTokens[4]); // // buffer.set_Value(buffer.Fields.FindField("TFH"), lastTokens[5]); // // buffer.set_Value(buffer.Fields.FindField("DKYT"), lastTokens[6]); // // buffer.set_Value(buffer.Fields.FindField("DLBM"), lastTokens[7]); // // // FeatureCursor.InsertFeature(buffer); // // _featureCursor.InsertFeature(buffer); // // pgCount++; // //} // #endregion // lastTokens = tokens; // } // else // { // #region // if (tokens.Length == 4) // { // IPoint pt = new PointClass(); // double x, y; // if ((!double.TryParse(tokens[2], out y) || !double.TryParse(tokens[3], out x)) || x < 40000000 || x > 41000000 || y < 2000000 || y > 4000000) // { // _errors.Add(string.Format("文件第{0}行的格式有误,坐标应为数字类型", row)); // polygon = new PolygonClass(); // pc = polygon as IPointCollection; // break; // } // pt.X = x; // pt.Y = y; // pc.AddPoint(pt, ref missing, ref missing); // list.Add(pt); // } // else // { // _errors.Add(string.Format("文件第{0}行的格式有误,字段数量应为4", row)); // polygon = new PolygonClass(); // ring = new RingClass(); // pc = ring as IPointCollection; // break; // } // #endregion // } // } // line = reader.ReadLine(); // row++; // } // try // { // IGeometryBridge2 geometryBridge2 = new GeometryEnvironmentClass(); // IPointCollection4 pcollection = new PolygonClass(); // WKSPoint[] wksPoint = new WKSPoint[list.Count]; // for (var i = 0; i < list.Count; i++) // { // var temp = list[i]; // wksPoint[i].X = temp.X; // wksPoint[i].Y = temp.Y; // } // geometryBridge2.SetWKSPoints(pcollection, ref wksPoint); // IPolygon poly = pcollection as IPolygon; // poly.Close(); // var buffer = FeatureClass.CreateFeatureBuffer(); // buffer.Shape = poly; // buffer.set_Value(buffer.Fields.FindField("FILENAME"), System.IO.Path.GetFileName(item)); // buffer.set_Value(buffer.Fields.FindField("DKBH"), lastTokens[2]); // buffer.set_Value(buffer.Fields.FindField("DKMC"), lastTokens[3]); // buffer.set_Value(buffer.Fields.FindField("JLTXSX"), lastTokens[4]); // buffer.set_Value(buffer.Fields.FindField("TFH"), lastTokens[5]); // buffer.set_Value(buffer.Fields.FindField("DKYT"), lastTokens[6]); // buffer.set_Value(buffer.Fields.FindField("DLBM"), lastTokens[7]); // _featureCursor.InsertFeature(buffer); // } // catch(Exception ex) // { // System.Diagnostics.Trace.WriteLine(ex); // } // //if (pc.PointCount > 0) // //{ // // if (pc.PointCount < 4) // // { // // _errors.Add(string.Format("文件第{0}行之前的坐标串有误,多边形顶点不应少于4个", row)); // // } // // else // // { // // polygon.AddGeometry(ring, ref missing, ref missing); // // } // //} // //if (polygon.GeometryCount > 0) // //{ // // #region // // try // // { // // var buffer = FeatureClass.CreateFeatureBuffer(); // // polygon.ITopologicalOperator2_IsKnownSimple_2 = false; // // (polygon as ITopologicalOperator2).Simplify(); // // polygon.GeometriesChanged(); // // buffer.Shape = polygon; // // buffer.set_Value(buffer.Fields.FindField("FILENAME"), System.IO.Path.GetFileName(item)); // // buffer.set_Value(buffer.Fields.FindField("DKBH"), lastTokens[2]); // // buffer.set_Value(buffer.Fields.FindField("DKMC"), lastTokens[3]); // // buffer.set_Value(buffer.Fields.FindField("JLTXSX"), lastTokens[4]); // // buffer.set_Value(buffer.Fields.FindField("TFH"), lastTokens[5]); // // buffer.set_Value(buffer.Fields.FindField("DKYT"), lastTokens[6]); // // buffer.set_Value(buffer.Fields.FindField("DLBM"), lastTokens[7]); // // _featureCursor.InsertFeature(buffer); // // pgCount++; // // } // // catch (Exception ex) // // { // // _errors.Add(string.Format("导入第{0}个地块时发生错误:{1}", pgCount, ex.Message)); // // } // // #endregion // //} //} //#endregion #region 方法一 using (var reader = new StreamReader(item, Encoding.GetEncoding("GB2312"))) { var line = reader.ReadLine(); var begin = false; var polygon = new PolygonClass(); IGeometryCollection resultPolygon = new PolygonClass(); var ring = new RingClass(); var pc = ring as IPointCollection; var ringId = string.Empty; var lastTokens = new[] { "", "", "", "", "", "", "", "", "" }; var row = 1; var pgCount = 0; var ext = false; while (line != null) { line = line.Trim(); if (line == "[地块坐标]") { begin = true; line = reader.ReadLine(); row++; ext = true; continue; } if (begin && !string.IsNullOrEmpty(line)) { string[] tokens = line.Split(','); if (tokens[tokens.Length - 1] == "@") { #region if (tokens.Length != 9) { _errors.Add(string.Format("文件第{0}行的格式有误,字段数量应为9", row)); break; } if (pc.PointCount > 0) { if (pc.PointCount < 4) { _errors.Add(string.Format("文件第{0}行之前的坐标串有误,多边形顶点数不应少于4个", row)); ring = new RingClass(); pc = ring as IPointCollection; break; } else { polygon.AddGeometry(ring, ref missing, ref missing); ring = new RingClass(); pc = ring as IPointCollection; } } if (polygon.GeometryCount > 0) { polygon.ITopologicalOperator2_IsKnownSimple_2 = false; (polygon as ITopologicalOperator2).Simplify(); polygon.GeometriesChanged(); resultPolygon.AddGeometryCollection(polygon as IGeometryCollection); } //if (polygon.GeometryCount > 0) //{ // var buffer = FeatureClass.CreateFeatureBuffer(); // polygon.ITopologicalOperator2_IsKnownSimple_2 = false; // (polygon as ITopologicalOperator2).Simplify(); // polygon.GeometriesChanged(); // buffer.Shape = polygon; // buffer.set_Value(buffer.Fields.FindField("FILENAME"), System.IO.Path.GetFileName(item)); // buffer.set_Value(buffer.Fields.FindField("DKBH"), lastTokens[2]); // buffer.set_Value(buffer.Fields.FindField("DKMC"), lastTokens[3]); // buffer.set_Value(buffer.Fields.FindField("JLTXSX"), lastTokens[4]); // buffer.set_Value(buffer.Fields.FindField("TFH"), lastTokens[5]); // buffer.set_Value(buffer.Fields.FindField("DKYT"), lastTokens[6]); // buffer.set_Value(buffer.Fields.FindField("DLBM"), lastTokens[7]); // // FeatureCursor.InsertFeature(buffer); // _featureCursor.InsertFeature(buffer); // pgCount++; //} #endregion lastTokens = tokens; } else { #region if (tokens.Length == 4) { IPoint pt = new PointClass(); double x, y; if (tokens[1] != ringId) { if (pc.PointCount > 0) { polygon.AddGeometry(ring, ref missing, ref missing); //resultPolygon.AddGeometryCollection(ring as IGeometryCollection); } ring = new RingClass(); pc = ring as IPointCollection; ringId = tokens[1]; } if ((!double.TryParse(tokens[2], out y) || !double.TryParse(tokens[3], out x)) || x < 40000000 || x > 41000000 || y < 2000000 || y > 4000000) { _errors.Add(string.Format("文件第{0}行的格式有误,坐标应为数字类型", row)); polygon = new PolygonClass(); pc = polygon as IPointCollection; break; } pt.X = x; pt.Y = y; pc.AddPoint(pt, ref missing, ref missing); } else { _errors.Add(string.Format("文件第{0}行的格式有误,字段数量应为4", row)); polygon = new PolygonClass(); ring = new RingClass(); pc = ring as IPointCollection; break; } #endregion } } line = reader.ReadLine(); row++; } if (pc.PointCount > 0) { if (pc.PointCount < 4) { _errors.Add(string.Format("文件第{0}行之前的坐标串有误,多边形顶点不应少于4个", row)); } else { polygon.AddGeometry(ring, ref missing, ref missing); ring = new RingClass(); pc = ring as IPointCollection; //resultPolygon.AddGeometryCollection(ring as IGeometryCollection); } } if (polygon.GeometryCount > 0) { #region try { var buffer = FeatureClass.CreateFeatureBuffer(); polygon.ITopologicalOperator2_IsKnownSimple_2 = false; (polygon as ITopologicalOperator2).Simplify(); polygon.GeometriesChanged(); resultPolygon.AddGeometryCollection(polygon as IGeometryCollection); ITopologicalOperator top = resultPolygon as ITopologicalOperator; top.Simplify(); // buffer.Shape = resultPolygon as IPolygon; buffer.Shape = resultPolygon as IPolygon; buffer.set_Value(buffer.Fields.FindField("FILENAME"), System.IO.Path.GetFileName(item)); buffer.set_Value(buffer.Fields.FindField("DKBH"), lastTokens[2]); buffer.set_Value(buffer.Fields.FindField("DKMC"), lastTokens[3]); buffer.set_Value(buffer.Fields.FindField("JLTXSX"), lastTokens[4]); buffer.set_Value(buffer.Fields.FindField("TFH"), lastTokens[5]); buffer.set_Value(buffer.Fields.FindField("DKYT"), lastTokens[6]); buffer.set_Value(buffer.Fields.FindField("DLBM"), lastTokens[7]); _featureCursor.InsertFeature(buffer); pgCount++; } catch (Exception ex) { _errors.Add(string.Format("导入第{0}个地块时发生错误:{1}", pgCount, ex.Message)); } #endregion } } #endregion } Finish(); return(true); }