arcgisEngine使用GP将cad转换为shp,shp转为json文件

这段代码展示了如何使用C#将CAD文件转换为SHP文件,定义坐标系,然后获取最大面积特征的FID,并将结果转换为JSON。主要涉及ArcGIS API的使用,包括CopyFeatures、DefineProjection、FeaturesToJSON等工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }
    }
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值