码迷,mamicode.com
首页 > 其他好文 > 详细

PIE SDK的矢量裁剪矢量文件的Algo

时间:2019-12-12 18:02:12      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:pat   not   algo   except   geometry   str   NPU   creates   thread   

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PIE.SystemAlgo;
using PIE.Carto;
using System.IO;
using PIE.DataSource;
using PIE.Geometry;

namespace Algo
{
/// <summary>
/// 用矢量文件裁剪矢量文件
/// </summary>
public class VectorClipAlgo:ISystemAlgo,ISystemAlgoEvents
{
private AlgoParms m_params;
//
// 摘要:
// 描述
public string Description { get; set; }
//
// 摘要:
// 名称
public string Name { get; set; }

public object Params
{
get
{
return m_params;
}

set
{
if (value ==null)
{
return;
}
m_params = (AlgoParms)value;
}
}

public VectorClipAlgo()
{

}

public event OnProgressChangedEventHandler OnProgressChanged;
public event OnExecuteCompletedEventHandler OnExecuteCompleted;

public void GetErrorInfo(ref int errCode, ref string errMsg)
{
throw new NotImplementedException();
}

public void SetErrorInfo(int errCode, string errMsg)
{
throw new NotImplementedException();
}

public bool Execute()
{
try
{
if (!File.Exists(m_params.InputFilePath)|| !File.Exists(m_params.ShpFilePath))//如果不存在,或者不是shp文件
{
return false;
}
ILayer layer = LayerFactory.CreateDefaultLayer(m_params.InputFilePath);
ILayer flayer = LayerFactory.CreateDefaultLayer(m_params.ShpFilePath);
if (layer == null|| flayer == null)
{
return false;
}
IFeatureDataset fdataset = (layer as IFeatureLayer).FeatureClass.GetFeatureDataset();
if (m_params.Type==0)
{
ClipByShpIn(fdataset, flayer);
}
else//type==1外切
{
ClipByShpOut(fdataset, flayer);
}
fdataset.Save();
fdataset.Copy(m_params.OutputFilePath);
((IDisposable)fdataset).Dispose();
fdataset = null;
return false;
}
catch (Exception)
{
return false;
}

}

/// <summary>
/// 矢量裁切矢量文件(内切)
/// </summary>
private void ClipByShpIn(IFeatureDataset fdataset, ILayer flayer)
{
try
{
Geometry cngeo = (flayer as IFeatureLayer).FeatureClass.GetFeature(0).Geometry as Geometry;
IFeatureClass fclass = new FeatureClass(fdataset);
long fcount = fclass.GetFeatureCount();
List<IGeometry> listgeos = new List<IGeometry>();
ISpatialReference spGLL = SpatialReferenceFactory.CreateSpatialReference(4326);
for (int i = 0; i < fcount; i++)
{
IFeature feature = fclass.GetFeature(i);
if (feature == null)
{
continue;
}
IGeometry geos = feature.Geometry as Geometry;
geos.SpatialReference = spGLL;
if (geos.GetGeometryType() == GeometryType.GeometryMultiPolygon)
{
for (int h = 0; h < (geos as IGeometryCollection).GetGeometryCount(); h++)
{
IGeometry geo = (geos as MultiPolygon).GetGeometry(h);
geo.SpatialReference = spGLL;
IGeometry ingeo = cngeo.Intersection(geo);
bool flag = cngeo.Intersects(geo);
if (ingeo != null)
{
listgeos.Add(ingeo);
}
}
}
else
{
IGeometry ingeo = (cngeo as Geometry).Intersection(geos);
if (ingeo != null)
{
listgeos.Add(ingeo);
}
}
fdataset.DeleteFeature(i);
}
for (int i = 0; i < listgeos.Count; i++)
{
IFeature feature = fdataset.CreateNewFeature();
feature.Geometry = listgeos[i];
fdataset.AddFeature(feature);
}

}
catch (Exception)
{

}


}


/// <summary>
/// 矢量裁切矢量文件(外切)
/// </summary>
private void ClipByShpOut(IFeatureDataset fdataset, ILayer flayer)
{
try
{

Geometry cngeo = (flayer as IFeatureLayer).FeatureClass.GetFeature(0).Geometry as Geometry;
IFeatureClass fclass = new FeatureClass(fdataset);
List<IGeometry> listgeos = new List<IGeometry>();
List<int> ss = new List<int>();
long fcount = fclass.GetFeatureCount();
ISpatialReference spGLL = SpatialReferenceFactory.CreateSpatialReference(4326);
for (int i = 0; i < fcount; i++)
{
IFeature feature = fclass.GetFeature(i);
if (feature == null)
{
continue;
}
IGeometry geo = feature.Geometry as Geometry;

geo.SpatialReference = spGLL;
if (!(geo as Geometry).Within(cngeo))//如果不在内部
{
IGeometry ingeo = cngeo.Intersection(geo);
if (ingeo == null)
{
listgeos.Add(geo);
fdataset.DeleteFeature(i);
}
else
{
IGeometry outgeo = (geo as Geometry).Difference(ingeo as Geometry);
listgeos.Add(outgeo);
fdataset.DeleteFeature(i);
}
}
else
{
fdataset.DeleteFeature(i);
}

}
for (int i = 0; i < listgeos.Count; i++)
{
IFeature feature = fdataset.CreateNewFeature();
feature.Geometry = listgeos[i];
fdataset.AddFeature(feature);
}
listgeos.Clear();
}
catch (Exception)
{

}


}
public void RaiseExecuteCompletedEvent()
{
throw new NotImplementedException();
}
}


public class AlgoParms
{
/// <summary>
/// 输入的shp路径
/// </summary>
public string InputFilePath { get; set; }

/// <summary>
/// 0裁剪shp内部,1裁剪shp外部
/// </summary>
public int Type { get; set; }

/// <summary>
/// 需要输入的shp文件路径
/// </summary>
public string ShpFilePath { get; set; }

/// <summary>
/// 需要输出的shp文件路径
/// </summary>
public string OutputFilePath { get; set; }
/// <summary>
/// 功能名称
/// </summary>
public string FuncName { get; set; }
}
}

PIE SDK的矢量裁剪矢量文件的Algo

标签:pat   not   algo   except   geometry   str   NPU   creates   thread   

原文地址:https://www.cnblogs.com/Smile0709/p/12030664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!