ArcGIS Server REST的JSON格式的数据生成SHP文件
--------------------------------------------------------------------------------------------------------------------------------------------------
AccessOperate.cs
--------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesFile;
using System.Collections;
using ESRI.ArcGIS.Geometry;
using System.Web.Script.Serialization;
using System.IO;
using ESRI.ArcGIS.esriSystem;
using System.Threading;
namespace ArcObjectsLibrary
{
public class AccessOperate
{
public AccessOperate()
{
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);
}
public IWorkspace OpenAccessWorkspace(string connString)
{
IWorkspace pWorkspace = null;
IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactory();
pWorkspace = pWorkspaceFactory.OpenFromFile(connString, 0);
return pWorkspace;
}
public IWorkspace OpenShapfileWorkspace(string Location)
{
IWorkspace pWorkspace = null;
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
pWorkspace = pWorkspaceFactory.OpenFromFile(Location, 0);
return pWorkspace;
}
public IWorkspace OpenFileGDBWorkspace(string Location)
{
IWorkspace pWorkspace = null;
IWorkspaceFactory pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory();
pWorkspace = pWorkspaceFactory.OpenFromFile(Location, 0);
return pWorkspace;
}
public String createSegment(string Location, String segmentRiverArrayString, String sceneId)
{
IWorkspace pWorkspace = OpenFileGDBWorkspace(Location);
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
String result = createSegmentAllByCoordinateAccordingRiver2(pFeatureWorkspace, segmentRiverArrayString, sceneId);
return result;
}
public String createSegmentAllByCoordinateAccordingRiver(IFeatureWorkspace pFeatureWorkspace, String segmentRiverArrayString, String sceneId)
{
String result = "successed";
String[] segmentRiverArray = segmentRiverArrayString.Split('|');
IList<IPolygon[]> pPolygonArrayList = new List<IPolygon[]>();
IList<String[]> codeArrayList = new List<String[]>();
//Single
IFeatureClass pFeatureClassRiverSinglePolyline = pFeatureWorkspace.OpenFeatureClass("河流单线");
ISpatialFilter pSpatialFilterRiverSinglePolyline = new SpatialFilter();
pSpatialFilterRiverSinglePolyline.GeometryField=(pFeatureClassRiverSinglePolyline.ShapeFieldName);
//Double
IFeatureClass pFeatureClassRiverDoublePolyline = pFeatureWorkspace.OpenFeatureClass("河流边线");
ISpatialFilter pSpatialFilterRiverDoublePolyline = new SpatialFilter();
pSpatialFilterRiverDoublePolyline.GeometryField=(pFeatureClassRiverDoublePolyline.ShapeFieldName);
//Polygon
IFeatureClass pFeatureClassRiverPolygon = pFeatureWorkspace.OpenFeatureClass("河流面");
ISpatialFilter pSpatialFilterRiverPolygon = new SpatialFilter();
pSpatialFilterRiverPolygon.GeometryField=(pFeatureClassRiverPolygon.ShapeFieldName);
//Segment
IFeatureClass pFeatureClassRiverSegment = pFeatureWorkspace.OpenFeatureClass("河流切片");
//
double distanceFromCurve = 0;
Boolean asRatio = false;
Boolean bRightSide = false;
IPoint pPointClosest = new Point();
if (segmentRiverArrayString.Length > 0)
{
for (int riverIndex = 0; riverIndex < segmentRiverArray.Length; ++riverIndex)
{
String segmentRiverString = segmentRiverArray[riverIndex];
String[] segmentRiverInfo = segmentRiverString.Split('$');
String riverCode = segmentRiverInfo[0];
String cordinateCodeArrayString = segmentRiverInfo[1];
String[] cordinateCodeArray = cordinateCodeArrayString.Split(':');
IPoint[] pPointArray = new IPoint[cordinateCodeArray.Length];
double[] lengthArray = new double[cordinateCodeArray.Length];
String[] codeArray = new String[cordinateCodeArray.Length];
IPolygon[] pPolygonArray = new IPolygon[cordinateCodeArray.Length];
for (int index = 0; index < cordinateCodeArray.Length; ++index)
{
String cordinateCodeString = cordinateCodeArray[index];
if (cordinateCodeString.Length > 0)
{
String[] coordinateCode = cordinateCodeString.Split(',');
String code = coordinateCode[0];
codeArray[index] = code;
String coordinateString = coordinateCode[1];
String[] coordinate = coordinateString.Split(' ');
double x = Double.Parse(coordinate[0]);
double y = Double.Parse(coordinate[1]);
IPoint pPoint = new Point();
pPoint.X = (x);
pPoint.Y = (y);
pPointArray[index] = pPoint;
}
}
//Single
pSpatialFilterRiverSinglePolyline.WhereClause=("CODE='" + riverCode + "'");
pSpatialFilterRiverSinglePolyline.SearchOrder=esriSearchOrder.esriSearchOrderAttribute;
IFeatureCursor pFeatureCursorRiverSinglePolyline = pFeatureClassRiverSinglePolyline.Search(pSpatialFilterRiverSinglePolyline, false);
IFeature pFeatureRiverSinglePolyline = pFeatureCursorRiverSinglePolyline.NextFeature();
IPolyline pPolylineSingleRiver = (IPolyline)pFeatureRiverSinglePolyline.Shape;
//Double
pSpatialFilterRiverDoublePolyline.WhereClause = ("CODE='" + riverCode + "'");
pSpatialFilterRiverDoublePolyline.SearchOrder = esriSearchOrder.esriSearchOrderAttribute;
IFeatureCursor pFeatureCursorRiverDoublePolyline = pFeatureClassRiverDoublePolyline.Search(pSpatialFilterRiverDoublePolyline, false);
IFeature pFeatureRiverDoubleOnePolyline = pFeatureCursorRiverDoublePolyline.NextFeature();
IPolyline pPolylineDoubleOneRiver = (IPolyline)pFeatureRiverDoubleOnePolyline.Shape;
IFeature pFeatureRiverDoubleTwoPolyline = pFeatureCursorRiverDoublePolyline.NextFeature();
IPolyline pPolylineDoubleTwoRiver = (IPolyline)pFeatureRiverDoubleTwoPolyline.Shape;
//Polygon
pSpatialFilterRiverPolygon.WhereClause = ("CODE='" + riverCode + "'");
pSpatialFilterRiverPolygon.SearchOrder = esriSearchOrder.esriSearchOrderAttribute;
IFeatureCursor pFeatureCursorRiverPolygon = pFeatureClassRiverPolygon.Search(pSpatialFilterRiverPolygon, false);
IFeature pFeatureRiverPolygon = pFeatureCursorRiverPolygon.NextFeature();
IPolygon pPolygonRiver = (IPolygon)pFeatureRiverPolygon.Shape;
ITopologicalOperator pITopologicalOperator = (ITopologicalOperator)pPolygonRiver;
IRelationalOperator pRelationalOperator2 = (IRelationalOperator)pPolygonRiver;
for (int index = 0; index < pPointArray.Length; ++index)
{
IPoint pPoint = pPointArray[index];
double distanceAlongCurve = 0;
pPolylineSingleRiver.QueryPointAndDistance(0, pPoint, asRatio, pPointClosest, distanceAlongCurve, distanceFromCurve, bRightSide);
lengthArray[index] = distanceAlongCurve;
}
for (int i = 0; i < lengthArray.Length - 1; i++)
{
for (int j = i + 1; j < lengthArray.Length; j++)
{
if (lengthArray[i] > lengthArray[j])
{
double temp = lengthArray[i];
lengthArray[i] = lengthArray[j];
lengthArray[j] = temp;
//
IPoint pPoint = pPointArray[i];
pPointArray[i] = pPointArray[j];
pPointArray[j] = pPoint;
//
String code = codeArray[i];
codeArray[i] = codeArray[j];
codeArray[j] = code;
}
}
}
for (int index = 0; index < pPointArray.Length; ++index)
{
IPoint pPointStart = null;
IPoint pPointEnd = null;
IPoint pPointCurrent = pPointArray[index];
if (index == 0)
{
pPointStart = pPointCurrent;
IPoint pPointNext = pPointArray[index + 1];
pPointEnd = new Point();
pPointEnd.X = ((pPointCurrent.X + pPointNext.X) / 2);
pPointEnd.Y = ((pPointCurrent.Y + pPointNext.Y) / 2);
}
else if (index == pPointArray.Length - 1)
{
pPointEnd = pPointCurrent;
IPoint pPointPrevious = pPointArray[index - 1];
pPointStart = new Point();
pPointStart.X=((pPointCurrent.X + pPointPrevious.X) / 2);
pPointStart.Y=((pPointCurrent.Y + pPointPrevious.Y) / 2);
}
else
{
pPointStart = new Point();
pPointEnd = new Point();
IPoint pPointPrevious = pPointArray[index - 1];
IPoint pPointNext = pPointArray[index + 1];
pPointStart.X=((pPointCurrent.X + pPointPrevious.X) / 2);
pPointStart.Y=((pPointCurrent.Y + pPointPrevious.Y) / 2);
pPointEnd.X=((pPointCurrent.X + pPointNext.X) / 2);
pPointEnd.Y=((pPointCurrent.Y + pPointNext.Y) / 2);
}
//
IPolyline pPolylineStart = contructPolyline(pPointStart, pPolylineDoubleOneRiver, pPolylineDoubleTwoRiver, pRelationalOperator2);
IPointCollection pPointCollectionStart = (IPointCollection)pPolylineStart;
IPoint pPointStartBegin = pPointCollectionStart.get_Point(0);
IPolyline pPolylineEnd = contructPolyline(pPointEnd, pPolylineDoubleOneRiver, pPolylineDoubleTwoRiver, pRelationalOperator2);
IPointCollection pPointCollectionEnd = (IPointCollection)pPolylineEnd;
IPoint pPointEndBegin = pPointCollectionEnd.get_Point(0);
IPoint pPointEndLast = pPointCollectionEnd.get_Point(1);
IPoint pPointEndMiddle = new Point();
pPointEndMiddle.X=((pPointEndBegin.X + pPointEndLast.X) / 2);
pPointEndMiddle.Y=((pPointEndBegin.Y + pPointEndLast.Y) / 2);
IGeometry pGeometryStartLeft = null;
IGeometry pGeometryStartRight = null;
IGeometry pGeometryEndLeft = null;
IGeometry pGeometryEndRight = null;
pITopologicalOperator.Cut(pPolylineStart, out pGeometryStartLeft, out pGeometryStartRight);
IPolygon pPolygonStartLeft = (IPolygon)pGeometryStartLeft;
IPolygon pPolygonStartRight = (IPolygon)pGeometryStartRight;
IRelationalOperator2 pRelationalOperator2StartRight = (IRelationalOperator2)pPolygonStartRight;
Boolean isContained = pRelationalOperator2StartRight.Contains(pPointEndMiddle);
IPolygon pPolygonResult = null;
ITopologicalOperator pTopologicalOperatorSecond = null;
if (isContained == true)
{
pTopologicalOperatorSecond = (ITopologicalOperator)pPolygonStartRight;
}
else
{
isContained =pRelationalOperator2StartRight.Crosses(pPolylineEnd);
if (isContained == true)
{
pTopologicalOperatorSecond = (ITopologicalOperator)pPolygonStartRight;
}
else
{
pTopologicalOperatorSecond = (ITopologicalOperator)pPolygonStartLeft;
}
}
pTopologicalOperatorSecond.Cut(pPolylineEnd, out pGeometryEndLeft, out pGeometryEndRight);
if (isContained == true)
{
pPolygonResult = (IPolygon)pGeometryEndLeft;
}
else
{
pPolygonResult = (IPolygon)pGeometryEndRight;
}
pPolygonArray[index] = pPolygonResult;
}
codeArrayList.Add(codeArray);
pPolygonArrayList.Add(pPolygonArray);
}
IFields pFields = pFeatureClassRiverSegment.Fields;
int fieldIndexCode = pFields.FindField("CODE");
int fieldIndexSceneId = pFields.FindField("SceneID");
IDataset pDataset = (IDataset)pFeatureClassRiverSegment;
IWorkspace pWorkspace = pDataset.Workspace;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pWorkspace;
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.StartEditOperation();
//
IFeatureBuffer pFeatureBuffer = null;
IFeatureCursor pFeatureCuror = null;
pFeatureCuror = pFeatureClassRiverSegment.Insert(true);
for (int riverIndex = 0; riverIndex < pPolygonArrayList.Count; ++riverIndex)
{
IPolygon[] pPolygonArray = (IPolygon[])pPolygonArrayList[riverIndex];
String[] codeArray = (String[])codeArrayList[riverIndex];
for (int index = 0; index < pPolygonArray.Length; ++index)
{
IPolygon pPolygon = pPolygonArray[index];
String code = codeArray[index];
pFeatureBuffer = pFeatureClassRiverSegment.CreateFeatureBuffer();
pFeatureBuffer.Shape=(pPolygon);
pFeatureBuffer.set_Value(fieldIndexCode, code);
pFeatureBuffer.set_Value(fieldIndexSceneId, sceneId);
pFeatureCuror.InsertFeature(pFeatureBuffer);
}
}
pFeatureCuror.Flush();
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);
result = "failed";
}
return result;
}
public String createSegmentAllByCoordinateAccordingRiver2(IFeatureWorkspace pFeatureWorkspace, String segmentRiverArrayString, String sceneId)
{
String result = "successed";
int number = 0;
String[] segmentRiverArray = segmentRiverArrayString.Split('|');
IList<IPolygon[]> pPolygonArrayList = new List<IPolygon[]>();
IList<String[]> codeArrayList = new List<String[]>();
//Single
IFeatureClass pFeatureClassRiverSinglePolyline = pFeatureWorkspace.OpenFeatureClass("河流单线");
ISpatialFilter pSpatialFilterRiverSinglePolyline = new SpatialFilter();
pSpatialFilterRiverSinglePolyline.GeometryField=(pFeatureClassRiverSinglePolyline.ShapeFieldName);
//Double
IFeatureClass pFeatureClassRiverDoublePolyline = pFeatureWorkspace.OpenFeatureClass("河流边线");
ISpatialFilter pSpatialFilterRiverDoublePolyline = new SpatialFilter();
pSpatialFilterRiverDoublePolyline.GeometryField=(pFeatureClassRiverDoublePolyline.ShapeFieldName);
//Polygon
IFeatureClass pFeatureClassRiverPolygon = pFeatureWorkspace.OpenFeatureClass("河流面");
ISpatialFilter pSpatialFilterRiverPolygon = new SpatialFilter();
pSpatialFilterRiverPolygon.GeometryField=(pFeatureClassRiverPolygon.ShapeFieldName);
//Segment
IFeatureClass pFeatureClassRiverSegment = pFeatureWorkspace.OpenFeatureClass("河流切片");
//
double distanceFromCurve = 0;
Boolean asRatio = false;
Boolean bRightSide = false;
IPoint pPointClosest = new Point();
if (segmentRiverArrayString.Length > 0)
{
for (int riverIndex = 0; riverIndex < segmentRiverArray.Length; ++riverIndex)
{
String segmentRiverString = segmentRiverArray[riverIndex];
String[] segmentRiverInfo = segmentRiverString.Split('$');
String riverCode = segmentRiverInfo[0];
String cordinateCodeArrayString = segmentRiverInfo[1];
String[] cordinateCodeArray = cordinateCodeArrayString.Split(':');
IPoint[] pPointArray = new IPoint[cordinateCodeArray.Length];
double[] lengthArray = new double[cordinateCodeArray.Length];
String[] codeArray = new String[cordinateCodeArray.Length];
IPolygon[] pPolygonArray = new IPolygon[cordinateCodeArray.Length];
for (int index = 0; index < cordinateCodeArray.Length; ++index)
{
String cordinateCodeString = cordinateCodeArray[index];
if (cordinateCodeString.Length > 0)
{
String[] coordinateCode = cordinateCodeString.Split(',');
String code = coordinateCode[0];
codeArray[index] = code;
String coordinateString = coordinateCode[1];
String[] coordinate = coordinateString.Split(' ');
double x = Double.Parse(coordinate[0]);
double y = Double.Parse(coordinate[1]);
IPoint pPoint = new Point();
pPoint.X = (x);
pPoint.Y = (y);
pPointArray[index] = pPoint;
}
}
//Single
pSpatialFilterRiverSinglePolyline.WhereClause=("CODE='" + riverCode + "'");
pSpatialFilterRiverSinglePolyline.SearchOrder=esriSearchOrder.esriSearchOrderAttribute;
IFeatureCursor pFeatureCursorRiverSinglePolyline = pFeatureClassRiverSinglePolyline.Search(pSpatialFilterRiverSinglePolyline, false);
IFeature pFeatureRiverSinglePolyline = pFeatureCursorRiverSinglePolyline.NextFeature();
IPolyline pPolylineSingleRiver = (IPolyline)pFeatureRiverSinglePolyline.Shape;
//Double
pSpatialFilterRiverDoublePolyline.WhereClause = ("CODE='" + riverCode + "'");
pSpatialFilterRiverDoublePolyline.SearchOrder = esriSearchOrder.esriSearchOrderAttribute;
IFeatureCursor pFeatureCursorRiverDoublePolyline = pFeatureClassRiverDoublePolyline.Search(pSpatialFilterRiverDoublePolyline, false);
IFeature pFeatureRiverDoubleOnePolyline = pFeatureCursorRiverDoublePolyline.NextFeature();
IPolyline pPolylineDoubleOneRiver = (IPolyline)pFeatureRiverDoubleOnePolyline.Shape;
IFeature pFeatureRiverDoubleTwoPolyline = pFeatureCursorRiverDoublePolyline.NextFeature();
IPolyline pPolylineDoubleTwoRiver = (IPolyline)pFeatureRiverDoubleTwoPolyline.Shape;
//Polygon
pSpatialFilterRiverPolygon.WhereClause = ("CODE='" + riverCode + "'");
pSpatialFilterRiverPolygon.SearchOrder = esriSearchOrder.esriSearchOrderAttribute;
IFeatureCursor pFeatureCursorRiverPolygon = pFeatureClassRiverPolygon.Search(pSpatialFilterRiverPolygon, false);
IFeature pFeatureRiverPolygon = pFeatureCursorRiverPolygon.NextFeature();
IPolygon pPolygonRiver = (IPolygon)pFeatureRiverPolygon.Shape;
ITopologicalOperator pITopologicalOperator = (ITopologicalOperator)pPolygonRiver;
IRelationalOperator pRelationalOperator2 = (IRelationalOperator)pPolygonRiver;
for (int index = 0; index < pPointArray.Length; ++index)
{
IPoint pPoint = pPointArray[index];
double distanceAlongCurve = 0;
pPolylineSingleRiver.QueryPointAndDistance(0, pPoint, asRatio, pPointClosest, distanceAlongCurve, distanceFromCurve, bRightSide);
lengthArray[index] = distanceAlongCurve;
}
for (int i = 0; i < lengthArray.Length - 1; i++)
{
for (int j = i + 1; j < lengthArray.Length; j++)
{
if (lengthArray[i] > lengthArray[j])
{
double temp = lengthArray[i];
lengthArray[i] = lengthArray[j];
lengthArray[j] = temp;
//
IPoint pPoint = pPointArray[i];
pPointArray[i] = pPointArray[j];
pPointArray[j] = pPoint;
//
String code = codeArray[i];
codeArray[i] = codeArray[j];
codeArray[j] = code;
}
}
}
IPolyline pPolylineStart = null;
IPolyline pPolylineEnd = null;
//中间的面
IPolygon pPolygonRiverBetweenFirstLast = null;
IPoint pPointStart = null;
IPoint pPointEnd = null;
// 处理开头部分
pPointStart = pPointArray[0];
IPolyline pPolylineFirst = contructPolyline(pPointStart, pPolylineDoubleOneRiver, pPolylineDoubleTwoRiver, pRelationalOperator2);
pPolylineStart = pPolylineFirst;
// 处理结尾部分
pPointEnd = pPointArray[pPointArray.Length - 1];
IPolyline pPolylineLast = contructPolyline(pPointEnd, pPolylineDoubleOneRiver, pPolylineDoubleTwoRiver, pRelationalOperator2);
//
IPointCollection pPointCollectionEnd = (IPointCollection)pPolylineLast;
IPoint pPointEndBegin = pPointCollectionEnd.get_Point(0);
IPoint pPointEndLast = pPointCollectionEnd.get_Point(1);
IPoint pPointEndMiddle = new Point();
pPointEndMiddle.X=((pPointEndBegin.X + pPointEndLast.X) / 2);
pPointEndMiddle.Y=((pPointEndBegin.Y + pPointEndLast.Y) / 2);
// 取出中间的面
IGeometry pGeometryStartLeft = null;
IGeometry pGeometryStartRight = null;
IGeometry pGeometryEndLeft = null;
IGeometry pGeometryEndRight = null;
pITopologicalOperator.Cut(pPolylineFirst, out pGeometryStartLeft, out pGeometryStartRight);
IPolygon pPolygonStartLeft = (IPolygon)pGeometryStartLeft;
IPolygon pPolygonStartRight = (IPolygon)pGeometryStartRight;
IRelationalOperator2 pRelationalOperator2StartRight = (IRelationalOperator2)pPolygonStartRight;
Boolean isContained = pRelationalOperator2StartRight.Contains(pPointEndMiddle);
ITopologicalOperator pTopologicalOperatorSecond = null;
if (isContained == true)
{
pTopologicalOperatorSecond = (ITopologicalOperator)pPolygonStartRight;
}
else
{
isContained = pRelationalOperator2StartRight.Crosses(pPolylineLast);
if (isContained == true)
{
pTopologicalOperatorSecond = (ITopologicalOperator)pPolygonStartRight;
}
else
{
pTopologicalOperatorSecond = (ITopologicalOperator)pPolygonStartLeft;
}
}
pTopologicalOperatorSecond.Cut(pPolylineLast, out pGeometryEndLeft, out pGeometryEndRight);
if (isContained == true)
{
pPolygonRiverBetweenFirstLast = (IPolygon)pGeometryEndLeft;
}
else
{
pPolygonRiverBetweenFirstLast = (IPolygon)pGeometryEndRight;
}
//
for (int index = 0; index < pPointArray.Length - 1; ++index)
{
pPointEnd = new Point();
pPointEnd.X=((pPointArray[index].X + pPointArray[index + 1].X) / 2);
pPointEnd.Y=((pPointArray[index].Y + pPointArray[index + 1].Y) / 2);
pPolylineEnd = contructPolyline(pPointEnd, pPolylineDoubleOneRiver, pPolylineDoubleTwoRiver, pRelationalOperator2);
pITopologicalOperator = (ITopologicalOperator)pPolygonRiverBetweenFirstLast;
pITopologicalOperator.Cut(pPolylineEnd, out pGeometryEndLeft, out pGeometryEndRight);
IPolygon pPolygonEndLeft = (IPolygon)pGeometryEndLeft;
IPolygon pPolygonEndRight = (IPolygon)pGeometryEndRight;
IRelationalOperator2 pRelationalOperator2EndRight = (IRelationalOperator2)pPolygonEndRight;
isContained = pRelationalOperator2EndRight.Disjoint(pPolylineStart);
IPolygon pPolygonResult = null;
if (isContained == true)
{
pPolygonRiverBetweenFirstLast = pPolygonEndRight;
pPolygonResult = pPolygonEndLeft;
}
else
{
pPolygonRiverBetweenFirstLast = pPolygonEndLeft;
pPolygonResult = pPolygonEndRight;
}
pPolygonArray[index] = pPolygonResult;
if (index == pPointArray.Length - 2)
{
pPolygonArray[index + 1] = pPolygonRiverBetweenFirstLast;
}
}
codeArrayList.Add(codeArray);
pPolygonArrayList.Add(pPolygonArray);
}
//添加要素
IFields pFields = pFeatureClassRiverSegment.Fields;
int fieldIndexCode = pFields.FindField("CODE");
int fieldIndexSceneId = pFields.FindField("SceneID");
IDataset pDataset = (IDataset)pFeatureClassRiverSegment;
IWorkspace pWorkspace = pDataset.Workspace;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pWorkspace;
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.StartEditOperation();
//
IFeatureBuffer pFeatureBuffer = null;
IFeatureCursor pFeatureCuror = null;
pFeatureCuror = pFeatureClassRiverSegment.Insert(true);
for (int riverIndex = 0; riverIndex < pPolygonArrayList.Count; ++riverIndex)
{
IPolygon[] pPolygonArray = (IPolygon[])pPolygonArrayList[riverIndex];
String[] codeArray = (String[])codeArrayList[riverIndex];
for (int index = 0; index < pPolygonArray.Length; ++index)
{
IPolygon pPolygon = pPolygonArray[index];
String code = codeArray[index];
pFeatureBuffer = pFeatureClassRiverSegment.CreateFeatureBuffer();
pFeatureBuffer.Shape = (pPolygon);
pFeatureBuffer.set_Value(fieldIndexCode, code);
pFeatureBuffer.set_Value(fieldIndexSceneId, sceneId);
pFeatureCuror.InsertFeature(pFeatureBuffer);
}
}
pFeatureCuror.Flush();
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);
result = "successed";
}
return result;
}
private IPolyline contructPolyline(IPoint pPointInput, IPolyline pPolylineSplitedOneSide, IPolyline pPolylineSplitedOtherSide, IRelationalOperator pRelationalOperator2)
{
Boolean asRatio = false;
double distanceAlongCurve = 0;
double distanceFromCurve = 0;
Boolean bRightSide = false;
IPoint pPointFirst = null;
IPoint pPointSecond = null;
IPolyline pPolylineReturn = null;
IPointCollection pPointCollection = null;
object obj = Type.Missing;
try
{
asRatio = false;
distanceAlongCurve = 0;
distanceFromCurve = 0;
bRightSide = false;
pPointFirst = new Point();
pPolylineSplitedOneSide.QueryPointAndDistance(0, pPointInput, asRatio, pPointFirst, distanceAlongCurve, distanceFromCurve, bRightSide);
asRatio = false;
distanceAlongCurve = 0;
distanceFromCurve = 0;
bRightSide = false;
pPointSecond = new Point();
pPolylineSplitedOtherSide.QueryPointAndDistance(0, pPointInput, asRatio, pPointSecond, distanceAlongCurve, distanceFromCurve, bRightSide);
pPolylineReturn = (new Polyline()) as IPolyline;
pPointCollection = (IPointCollection)pPolylineReturn;
pPointCollection.AddPoint(pPointFirst, ref obj, ref obj);
pPointCollection.AddPoint(pPointSecond, ref obj, ref obj);
}
catch (Exception e)
{
}
return pPolylineReturn;
}
public String readFile(String filePath)
{
if (!File.Exists(filePath))
{
throw new Exception("文件不存在!");
}
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader streamReader = new StreamReader(fileStream);
string jsonString = string.Empty;
jsonString = streamReader.ReadToEnd();
streamReader.Close();
fileStream.Close();
return jsonString;
}
public String inputArcGISServerJSON(String filePath)
{
String result = "failed";
String[] filePathArray = filePath.Split('\\');
String geodatabasePath = filePath + @"\access\";
String geodatabaseName = "NanJing";
String datasetName = filePathArray[filePathArray.Length-1];
DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
FileInfo[] fileInfo = directoryInfo.GetFiles();
//IFeatureDataset pFeatureDataset = CreateGeoDatabase(geodatabasePath, geodatabaseName, datasetName);
for (int fileIndex = 0; fileIndex < fileInfo.Length; fileIndex++)
{
String fileName = fileInfo[fileIndex].Name;
String featureClassName = fileName.Substring(0, fileName.Length - 4);
String jsonString = readFile(fileInfo[fileIndex].FullName);
System.Diagnostics.Debug.WriteLine(fileName);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue;
javaScriptSerializer.RecursionLimit = 20000000;
Dictionary<string, object> featureCollectionJSONString = (Dictionary<string, object>)javaScriptSerializer.DeserializeObject(jsonString);
object[] featureObjectArray = (object[])featureCollectionJSONString["features"];
Dictionary<string, object> fieldAliasesJSONObject = (Dictionary<string, object>)featureCollectionJSONString["fieldAliases"];
Dictionary<string, object>.KeyCollection keyCollection = fieldAliasesJSONObject.Keys;
String[] fieldNameArray = new String[keyCollection.Count];
for (int i = 0; i < keyCollection.Count; i++)
{
String key = keyCollection.ElementAt<string>(i);
fieldNameArray[i] = key;
}
//
String geometryType = featureCollectionJSONString["geometryType"].ToString();
esriGeometryType featureGeometryType = (esriGeometryType)Enum.Parse(typeof(esriGeometryType), geometryType);
//
//IFeatureClass pFeatureClass = CreateFeatureClass(pFeatureDataset, featureClassName, fieldNameArray, featureGeometryType);
IFeatureClass pFeatureClass = CreateFeatureClassSHP(geodatabasePath, featureClassName, fieldNameArray, featureGeometryType);
//
//添加要素
IFields pFields = pFeatureClass.Fields;
IDataset pDataset = (IDataset)pFeatureClass;
IWorkspace pWorkspace = pDataset.Workspace;
IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pWorkspace;
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.StartEditOperation();
//
IFeatureBuffer pFeatureBuffer = null;
IFeatureCursor pFeatureCuror = null;
pFeatureCuror = pFeatureClass.Insert(true);
//
for (int i = 0; i < featureObjectArray.Length; i++)
{
Dictionary<string, object> featureJSONObject = (Dictionary<string, object>)featureObjectArray[i];
Dictionary<string, object> attributesJSONObject = (Dictionary<string, object>)featureJSONObject["attributes"];
Dictionary<string, object> geometryJSONObject = (Dictionary<string, object>)featureJSONObject["geometry"];
IGeometry pGeometry = null;
int fieldIndex = 0;
String fieldValue = null;
pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
if (geometryType == "esriGeometryPolygon")
{
IPolygon pPolygon = null;
pPolygon = ArcGISServerJSONToPolygon(geometryJSONObject);
pGeometry = pPolygon;
}
else if (geometryType == "esriGeometryPolyline")
{
IPolyline pPolyline = null;
pPolyline = ArcGISServerJSONToPolyline(geometryJSONObject);
pGeometry = pPolyline;
}
else if (geometryType == "esriGeometryPoint")
{
IPoint pPoint = null;
pPoint = ArcGISServerJSONToPoint(geometryJSONObject);
pGeometry = pPoint;
}
//
pFeatureBuffer.Shape = (pGeometry);
for (int index = 0; index < fieldNameArray.Length; index++)
{
fieldIndex = pFields.FindField(fieldNameArray[index]);
//object obj = attributesJSONObject[fieldNameArray[index]];
//System.Diagnostics.Debug.WriteLine(obj.GetType().Name);
fieldValue = attributesJSONObject[fieldNameArray[index]].ToString();
pFeatureBuffer.set_Value(fieldIndex, fieldValue);
}
pFeatureCuror.InsertFeature(pFeatureBuffer);
System.Diagnostics.Debug.WriteLine(fieldValue);
}
pFeatureCuror.Flush();
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);
System.Diagnostics.Debug.WriteLine("----------" + fileName + "----------");
Thread.CurrentThread.Join(8000);
}
result = "successed";
return result;
}
public IFeatureDataset CreateGeoDatabase(String geodatabasePath,String geodatabaseName,String datasetName)
{
//IPropertySet pPropertySet = (new PropertySet()) as ESRI.ArcGIS.esriSystem.IPropertySet;
//pPropertySet.SetProperty("DATABASE", @"d:\NanJing.mdb");
IWorkspaceFactory pWorkspaceFactory = (new AccessWorkspaceFactory()) as IWorkspaceFactory; // ESRI.ArcGIS.DataSourcesGDB.InMemoryWorkspaceFactory();
IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create(geodatabasePath, geodatabaseName, null, 0);
IName pName = pWorkspaceName as IName;
IFeatureWorkspace pFeatureWorkspace = pName.Open() as IFeatureWorkspace;
//UnknownCoordinateSystem pUnknownCoordinateSystem = new ESRI.ArcGIS.Geometry.UnknownCoordinateSystem();
//ISpatialReference pSpatialReference = (new ESRI.ArcGIS.Geometry.UnknownCoordinateSystem()) as ISpatialReference;
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(2385); //esriSRProjCS_Xian1980_3_Degree_GK_CM_120E 2385 Xian 1980 3 Degree GK CM 120E.
IFeatureDataset pFeatureDataset = pFeatureWorkspace.CreateFeatureDataset(datasetName, pSpatialReference);
//
return pFeatureDataset;
//IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(p_InLayer.Name, p_Fields, null, null, esriFeatureType.esriFTSimple, pInFeatureClass.ShapeFieldName, "");
}
public IFeatureClass CreateFeatureClassSHP(String geodatabasePath, String featureClassName, String[] fieldName, esriGeometryType geometryType)
{
IFeatureWorkspace pFeatureWorkspace = null;
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(geodatabasePath, 0);
//
ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironment();
ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateProjectedCoordinateSystem(2385); //esriSRProjCS_Xian1980_3_Degree_GK_CM_120E 2385 Xian 1980 3 Degree GK CM 120E.
IFields pFields = (new Fields()) as IFields;
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IFieldEdit pFieldEdit = null;
IField pField = null;
//
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = geometryType; //
pGeometryDefEdit.SpatialReference_2 = pSpatialReference;
pField = new Field();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
//
for (int i = 0; i < fieldName.Length; i++)
{
pField = new Field();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = fieldName[i];
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.Length_2 = 80;
pFieldsEdit.AddField(pField);
}
//
IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(featureClassName, pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");
return pFeatureClass;
}
public IFeatureClass CreateFeatureClass(IFeatureDataset pFeatureDataset, String featureClassName, String[] fieldName, esriGeometryType geometryType)
{
IFields pFields = (new Fields()) as IFields;
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IFieldEdit pFieldEdit = null;
IField pField = null;
//
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = geometryType; //
pField = new Field();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
//
for (int i = 0; i < fieldName.Length; i++)
{
pField = new Field();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = fieldName[i];
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.Length_2 = 80;
pFieldsEdit.AddField(pField);
}
//
IFeatureClass pFeatureClass = pFeatureDataset.CreateFeatureClass(featureClassName, pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");
return pFeatureClass;
}
public IPolygon ArcGISServerJSONToPolygon(Dictionary<string, object> geometryJSONObject)
{
object missing = Type.Missing;
IPolygon pPolygon = (new Polygon()) as IPolygon;
IGeometryCollection pGeometryCollection = pPolygon as IGeometryCollection;
object[] ringObjectArray = (object[])geometryJSONObject["rings"];
for (int m = 0; m < ringObjectArray.Length; m++)
{
IRing pRing = (new Ring()) as IRing;
ISegmentCollection pSegmentCollection = pRing as ISegmentCollection;
object[] coordinatesArray = (object[])ringObjectArray[m];
for (int n = 0; n < coordinatesArray.Length-1; n++)
{
object[] coordinatesStart = (object[])coordinatesArray[n];
double xStart = Convert.ToDouble(coordinatesStart[0]);
double yStart = Convert.ToDouble(coordinatesStart[1]);
IPoint pPointStart = new Point();
pPointStart.X = xStart;
pPointStart.Y = yStart;
//
object[] coordinatesEnd = (object[])coordinatesArray[n+1];
double xEnd = Convert.ToDouble(coordinatesEnd[0]);
double yEnd = Convert.ToDouble(coordinatesEnd[1]);
IPoint pPointEnd = new Point();
pPointEnd.X = xEnd;
pPointEnd.Y = yEnd;
//
ILine pLine= new Line();
pLine.PutCoords(pPointStart, pPointEnd);
ISegment pSegment= pLine as ISegment;
pSegmentCollection.AddSegment(pSegment, missing, missing);
}
IGeometry pGeometry = pSegmentCollection as IGeometry;
pGeometryCollection.AddGeometry(pGeometry,missing,missing);
}
ITopologicalOperator pTopologicalOperator = pGeometryCollection as ITopologicalOperator;
pTopologicalOperator.Simplify();
pPolygon = pTopologicalOperator as IPolygon;
return pPolygon;
}
public IPolyline ArcGISServerJSONToPolyline(Dictionary<string, object> geometryJSONObject)
{
object missing = Type.Missing;
IPolyline pPolyline = (new Polyline()) as IPolyline;
IGeometryCollection pGeometryCollection = pPolyline as IGeometryCollection;
object[] pathObjectArray = (object[])geometryJSONObject["paths"];
//
for (int m = 0; m < pathObjectArray.Length; m++)
{
IPath pPath = (new ESRI.ArcGIS.Geometry.Path()) as IPath;
ISegmentCollection pSegmentCollection = pPath as ISegmentCollection;
object[] coordinatesArray = (object[])pathObjectArray[m];
for (int n = 0; n < coordinatesArray.Length - 1; n++)
{
object[] coordinatesStart = (object[])coordinatesArray[n];
double xStart = Convert.ToDouble(coordinatesStart[0]);
double yStart = Convert.ToDouble(coordinatesStart[1]);
IPoint pPointStart = new Point();
pPointStart.X = xStart;
pPointStart.Y = yStart;
//
object[] coordinatesEnd = (object[])coordinatesArray[n + 1];
double xEnd = Convert.ToDouble(coordinatesEnd[0]);
double yEnd = Convert.ToDouble(coordinatesEnd[1]);
IPoint pPointEnd = new Point();
pPointEnd.X = xEnd;
pPointEnd.Y = yEnd;
//
ILine pLine = new Line();
pLine.PutCoords(pPointStart, pPointEnd);
ISegment pSegment = pLine as ISegment;
pSegmentCollection.AddSegment(pSegment, missing, missing);
}
IGeometry pGeometry = pSegmentCollection as IGeometry;
pGeometryCollection.AddGeometry(pGeometry, missing, missing);
}
ITopologicalOperator pTopologicalOperator = pGeometryCollection as ITopologicalOperator;
pTopologicalOperator.Simplify();
pPolyline = pTopologicalOperator as IPolyline;
return pPolyline;
}
public IPoint ArcGISServerJSONToPoint(Dictionary<string, object> geometryJSONObject)
{
IPoint pPoint = (new Point()) as IPoint;
double x = Convert.ToDouble(geometryJSONObject["x"]);
double y = Convert.ToDouble(geometryJSONObject["y"]);
pPoint.X = x;
pPoint.Y = y;
return pPoint;
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------
Program.cs
--------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace YRWRPMS
{
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main()
{
//String path = @"D:\SpatialData\New folder\SimulationData.gdb";
//String encodeString = "ADA00000$55,114.0255744401191 34.95343793716928:56,114.06011713214326 34.94509351788156:57,114.07853395347342 34.95052352052007:58,114.10741864372679 34.95548485543207:59,114.14307599893378 34.94138488347235:60,114.18594328337642 34.93942429291288:61,114.22799079317176 34.907621536593055:62,114.27614924919774 34.90559551168073:63,114.31870182270816 34.92543177292525:64,114.36713724690924 34.917987860741604:65,114.41390220073671 34.90210956544993:66,114.43310158502315 34.92759780146053:67,114.45845174738679 34.92177178835249:68,114.48021623508104 34.92759434664814:69,114.50664220797309 34.91801318443139:70,114.53735713074501 34.90011532614997:71,114.57350916702137 34.924140211541676:72,114.61613879953254 34.91855676167968:73,114.65280759711024 34.92722549599616:74,114.67710388064934 34.90538689346031:75,114.710274194084 34.8898407525156:76,114.74366246200547 34.895702607518494:77,114.77664165547452 34.91436187156665:78,114.75669038073103 34.93748957880343:79,114.74073367223988 34.95287794181905:80,114.75827513271665 34.971710470135186:81,114.78878609063167 34.97864185249331:82,114.81815966226637 34.98816975881714:83,114.83080224701663 35.009454833104684:84,114.82126865892425 35.03943257295225:85,114.82963933928114 35.069315610544336:86,114.87003934013858 35.08352338587195:87,114.8792813648852 35.11364869618363:88,114.85553761377275 35.13815399965345:89,114.83494042335668 35.157000460420726:90,114.85067135823752 35.179910174769375:91,114.8793043585104 35.19174784367643:92,114.91753793771392 35.19459159099682:93,114.9242829508609 35.226237838997264:94,114.94009791506367 35.254308152564995:95,114.98511748259529 35.2564335960296:96,115.00144905860684 35.29172874442548:97,115.0116585190456 35.33063285830579:98,115.01515052989917 35.369588703383705:99,115.06169082756581 35.37352856417782:100,115.07716752305981 35.38752520537278:101,115.07769651991046 35.388415554570805:102,115.0846798967856 35.399591362811805:103,115.10467061281916 35.41544845026314:104,115.1331375883898 35.42166283901207:105,115.16190186390831 35.42656177641652:106,115.19159802978304 35.42171806601609:107,115.21485203323591 35.41150656813025:108,115.23995139032517 35.42805084235104:109,115.26399442288592 35.4457018565498:110,115.2842725392894 35.46793567789227:111,115.3132984592682 35.48399632106312:112,115.34557409926819 35.49635997790006:113,115.34398472595336 35.521224854375276:114,115.33516700337314 35.54839140460324:115,115.35838245545087 35.56814890365414:116,115.39035640406408 35.55892445295027:117,115.40249501645404 35.59014047376758:118,115.40875209923185 35.62019463875111:119,115.4115818978495 35.646209945279544:120,115.43680219988796 35.65700137265385:121,115.46436829603518 35.66772984318909:122,115.49091661160163 35.68157739272379:123,115.51595604343099 35.69611393075686:124,115.51904005791783 35.7192379995456:125,115.52281768712125 35.73942398795299:126,115.56229743000556 35.737226402999255:127,115.60598801884585 35.72592556116106:128,115.6449408294008 35.75153385499545"; //"ADA00000$p001,113.285 34.954:p002,113.356 34.969:p003,113.417 34.984:p004,113.497 34.962:p005,113.575 34.949";
ArcObjectsLibrary.AccessOperate accessOperate = new ArcObjectsLibrary.AccessOperate();
accessOperate.inputArcGISServerJSON(filePath);
}
}
}