using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.ConversionTools;
using System.IO;
using ESRI.ArcGIS.SOESupport;
namespace GetFeatureFromID
{
public static class ClsCadToShp
{
private static JsonObject json = null;
private static string result = string.Empty;
public static string GetResultJson(string dwgPath, string shpName)
{
string savefile = dwgPath;
string path = dwgPath.Substring(0, dwgPath.LastIndexOf("\\"));
string ReaderJson = "";
string strPath = path + "\\" + shpName + "1.json";
if (File.Exists(strPath))
{
//读取文件
StreamReader sr = new StreamReader(strPath, System.Text.Encoding.UTF8);
ReaderJson = sr.ReadToEnd();
sr.Close();
return ReaderJson;
}
else
{
Geoprocessor GP = new Geoprocessor();
#region 1、dwg转换为shp ******************************
try
{
GP.OverwriteOutput = true;
CopyFeatures mycopy = new CopyFeatures();
mycopy.in_features = dwgPath + "\\Polygon";
mycopy.out_feature_class = path + "\\" + shpName + ".shp";
GP.Execute(mycopy, null);
}
catch (Exception)
{
result = "{\"success\": false,\"msg\": \"转换shp报错。\"}";
json = new JsonObject();
json.AddString("result", result);
return result;
}
#endregion
#region 2、为shp复制坐标系 *******************************
try
{
DefineProjection myDP = new DefineProjection();
myDP.in_dataset = path + "\\" + shpName + ".shp";
myDP.coor_system = path + "\\touying.prj";
GP.Execute(myDP, null);
}
catch (Exception)
{
result = "{\"success\": false,\"msg\": \"赋值坐标系报错。\"}";
json = new JsonObject();
json.AddString("result", result);
return result;
}
#endregion
#region 3、获取面积最大的要素的fid ******************************
//double dArea = 0;
//int FID = 0;
//List<double> li = new List<double>();
//try
//{
// IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
// IWorkspace pWorkSpace = pWorkspaceFactory.OpenFromFile(path, 0);
// IFeatureWorkspace pFeatWorkSpace = pWorkSpace as IFeatureWorkspace;
// IFeatureClass pFeatureClass = pFeatWorkSpace.OpenFeatureClass(shpName);
// IFeatureCursor pFeatureCur = pFeatureClass.Search(null, false);
// IFeature pFeature = pFeatureCur.NextFeature();
// while (pFeature != null)
// {
// if (pFeature.Shape.GeometryType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
// {
// ESRI.ArcGIS.Geometry.IArea pArea = pFeature.Shape as ESRI.ArcGIS.Geometry.IArea;
// li.Add(pArea.Area);
// if (pArea.Area > dArea)
// {
// FID = int.Parse(pFeature.get_Value(0).ToString());
// dArea = pArea.Area;
// }
// }
// pFeature = pFeatureCur.NextFeature();
// }
//}
//catch (Exception)
//{
// result = "{\"success\": false,\"msg\": \"查询条件报错。\"}";
// json = new JsonObject();
// json.AddString("result", result);
// return result;
//}
#endregion
#region 4、根据查询条件获取指定要素feature **************************************
//try
//{
// Select select = new Select();
// select.in_features = path + "\\" + shpName + ".shp";
// select.out_feature_class = path + "\\" + shpName + "1.shp";
// select.where_clause = "FID=" + FID;
// GP.Execute(select, null);
//}
//catch (Exception)
//{
// result = "{\"success\": false,\"msg\": \"过滤图层报错。\"}";
// json = new JsonObject();
// json.AddString("result", result);
// return result;
//}
#endregion
#region 5、将要素转换为json ***************************************
try
{
string inputfile = path + "\\" + shpName + ".shp";
FeaturesToJSON featureToJson = new FeaturesToJSON(inputfile, path + "\\" + shpName + ".json");
featureToJson.in_features = inputfile;
featureToJson.out_json_file = path + "\\" + shpName + ".json";
GP.Execute(featureToJson, null);
}
catch (Exception)
{
result = "{\"success\": false,\"msg\": \"转换json报错。\"}";
json = new JsonObject();
json.AddString("result", result);
return result;
}
#endregion
if (File.Exists(strPath))
{
//读取文件
StreamReader sr = new StreamReader(strPath, System.Text.Encoding.UTF8);
ReaderJson = sr.ReadToEnd();
sr.Close();
return ReaderJson;
}
}
result = "{\"success\": false,\"msg\": \"其它错误。\"}";
json = new JsonObject();
json.AddString("result", result);
return result;
}
}
}