查看C#代码
1 //我最近的研究成果(IGeometry.Project and IGeometry.SpatialReference) 2 3 4 5 using System; 6 using System.Collections.Generic; 7 using System.ComponentModel; 8 using System.Data; 9 using System.Drawing; 10 using System.Linq; 11 using System.Text; 12 using System.Windows.Forms; 13 using System.Collections; 14 15 16 using ESRI.ArcGIS.Geodatabase; 17 using ESRI.ArcGIS.SystemUI; 18 using ESRI.ArcGIS.esriSystem; 19 using ESRI.ArcGIS.Carto; 20 using ESRI.ArcGIS.Controls; 21 using ESRI.ArcGIS.DataSourcesGDB; 22 using ESRI.ArcGIS.DataSourcesFile; 23 using ESRI.ArcGIS.Geometry; 24 using ESRI.ArcGIS.Display; 25 using ESRI.ArcGIS.AnalysisTools; //使用GP工具 26 using ESRI.ArcGIS.Geoprocessing; 27 using ESRI.ArcGIS.Geoprocessor; 28 29 30 31 namespace SearchAndDisplay 32 { 33 public partial class Form1 : Form 34 { 35 //传递数据之用 36 private IFeatureClass pCommonPolygonClass; 37 private IFeatureClass pCommonPolylineClass; 38 private IFeatureClass pCommonPointClass; 39 private IFeatureWorkspace pPublicFeatureWorkspace=null; 40 41 private IArray pArray; 42 private bool isFileExist; 43 private bool isGDBExist; 44 45 //传递数据库中的数据 46 private string XY; 47 private string Globle; 48 private double falseEasting; 49 private double falseNorthing; 50 private double centralMeridian; 51 private double scaleFactor; 52 private double latitudeOfOrigion; 53 54 private string[] XandY; 55 private IFeatureClass pPublicInMemoryClass; 56 public Form1() 57 { 58 InitializeComponent(); 59 } 60 61 //Create GCS:Beijing54 62 private ISpatialReference GetGCSBeijing54() 63 { 64 ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass(); 65 ISpatialReference pSR = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954); 66 pSR.SetDomain(-400,-400,400,400); 67 return pSR; 68 69 } 70 71 72 //Create GCS:Xi'an80 73 private ISpatialReference GetGCSXian80() 74 { 75 ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass(); 76 //pSRF.CreateProjectedCoordinateSystem(esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_19); 77 //pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980); 78 ISpatialReference pSR = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980); 79 pSR.SetDomain(-400, -400, 400, 400); 80 return pSR; 81 } 82 83 84 //Create GCS:WGS1984 85 private ISpatialReference GetGCSWGS1984() 86 { 87 ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass(); 88 ISpatialReference pSR = pSRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); 89 pSR.SetDomain(-400, -400, 400, 400); 90 return pSR; 91 } 92 //Create PCS:Xian80 zone19 93 private ISpatialReference GetPCSXian80() 94 { 95 ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass(); 96 ISpatialReference pSR = pSRF.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_GK_Zone_19); 97 pSR.SetDomain(13876800, -10002100, 900709923374.099, 900733802274.099); 98 return pSR; 99 } 100 101 102 //Create PCS:Beijng54 zone19 103 private ISpatialReference GetPCSBeijing54() 104 { 105 ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass(); 106 ISpatialReference pSR = pSRF.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_Beijing1954GK_19); 107 pSR.SetDomain(14876700, -10002300, 900734802174.099, 900709923174.099); 108 return pSR; 109 110 } 111 112 //Create PCS:WGS1984 zong49 113 private ISpatialReference GetPCSWGS1984() 114 { 115 ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass(); 116 ISpatialReference pSR = pSRF.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_49N); 117 pSR.SetDomain(-5120900, -9998100, 900714804574.099, 900709927374.099); 118 return pSR; 119 120 } 121 122 //Load 3 featureClasses with different spatial reference 123 private void Form1_Load(object sender, EventArgs e) 124 { 125 axMapControl1.Map.SpatialReference = GetPCSBeijing54(); 126 axMapControl2.Map.SpatialReference = GetPCSXian80(); 127 axMapControl3.Map.SpatialReference = GetPCSWGS1984(); 128 129 } 130 private void TabsChanged() 131 { 132 133 } 134 135 //add 3 layers to 3 mapcontrols with the different spatial reference 136 private void button8_Click(object sender, EventArgs e) 137 { 138 IPoint pPoint=null ; 139 IPolyline pL = null; 140 object _missing = Type.Missing; 141 142 143 IWorkspaceFactory pSdeWorkspaceFactory = new SdeWorkspaceFactory(); 144 IPropertySet pPropertySet= new PropertySetClass(); 145 pPropertySet.SetProperty("Server","siwei-gr7bzmd3q"); 146 pPropertySet.SetProperty("Instance","5008"); 147 pPropertySet.SetProperty("Database","sdeTest"); 148 pPropertySet.SetProperty("user","sa"); 149 pPropertySet.SetProperty("password","zhangjun1130"); 150 pPropertySet.SetProperty("version","sde.DEFAULT"); 151 IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pSdeWorkspaceFactory.Open(pPropertySet, 0); 152 pPublicFeatureWorkspace =pFeatureWorkspace; 153 154 IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pFeatureWorkspace; 155 IFeatureClass pFeatureClass; 156 IFeatureLayer pFeatureLayer; 157 158 pFeatureClass = pFeatureWorkspace.OpenFeatureClass("sdeTest.DBO.DomBeijing54"); 159 pFeatureLayer = new FeatureLayerClass(); 160 pFeatureLayer.FeatureClass = pFeatureClass; 161 axMapControl1.AddLayer(pFeatureLayer); 162 163 pFeatureClass = pFeatureWorkspace.OpenFeatureClass("sdeTest.DBO.DomXian80"); 164 pFeatureLayer = new FeatureLayerClass(); 165 pFeatureLayer.FeatureClass = pFeatureClass; 166 axMapControl2.AddLayer(pFeatureLayer); 167 168 pFeatureClass = pFeatureWorkspace.OpenFeatureClass("sdeTest.DBO.DomWGS84"); 169 pFeatureLayer = new FeatureLayerClass(); 170 pFeatureLayer.FeatureClass = pFeatureClass; 171 axMapControl3.AddLayer(pFeatureLayer); 172 173 174 175 176 #region 暂时不用 177 //// 摄区 178 //#region 搞摄区 179 //pWorkspaceEdit.StartEditing(true); 180 //pWorkspaceEdit.StartEditOperation(); 181 182 // IFeatureClass pPolygonsClassOld = pFeatureWorkspace.OpenFeatureClass("polygons"); 183 // ITable pTable = (ITable)pPolygonsClassOld; 184 // if (pTable != null) 185 // { 186 // pTable.DeleteSearchedRows(null); 187 188 // } 189 190 //IFeatureClass pPolygonsClass = pFeatureWorkspace.OpenFeatureClass("polygons"); 191 //isGDBExist = true; 192 193 //IFeature pPolygonsFeaure; 194 //pPolygonsFeaure= pPolygonsClass.CreateFeature(); 195 196 //IPolygon pPolygon = new PolygonClass(); 197 //IPointCollection pPointCollection = (IPointCollection)pPolygon; 198 //pPoint = new PointClass(); 199 //pPoint.PutCoords(10, 10); 200 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 201 //pPoint.PutCoords(15, 10); 202 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 203 //pPoint.PutCoords(15, 8); 204 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 205 //pPoint.PutCoords(18, 8); 206 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 207 //pPoint.PutCoords(18, 4); 208 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 209 //pPoint.PutCoords(10, 4); 210 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 211 //pPolygonsFeaure.Shape = pPolygon; 212 //pPolygon.Close(); 213 //pPolygonsFeaure.Store(); 214 215 216 217 //pPolygonsFeaure = pPolygonsClass.CreateFeature(); 218 //pPolygon = new PolygonClass(); 219 //pPointCollection = (IPointCollection)pPolygon; 220 //pPoint = new PointClass(); 221 //pPoint.PutCoords(20, 8); 222 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 223 //pPoint.PutCoords(25, 8); 224 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 225 //pPoint.PutCoords(25, 4); 226 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 227 //pPoint.PutCoords(20, 4); 228 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 229 //pPolygonsFeaure.Shape = pPolygon; 230 //pPolygon.Close(); 231 //pPolygonsFeaure.Store(); 232 233 234 //#endregion 235 236 //// 航线 237 //#region 搞航线 238 //IFeatureClass pPolylineClass = pFeatureWorkspace.OpenFeatureClass("polylines"); 239 //IFeature pPolylinesFeature = null; 240 241 //pPolylinesFeature = pPolylineClass.CreateFeature(); 242 //pL = new PolylineClass(); 243 //pPointCollection = (IPointCollection)pL; 244 //pPoint = new PointClass(); 245 //pPoint.PutCoords(9.5, 9.5); 246 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 247 //pPoint.PutCoords(15.5, 9.5); 248 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 249 //pPolylinesFeature.Shape = pL; 250 //pPolylinesFeature.Store(); 251 252 //pPolylinesFeature = pPolylineClass.CreateFeature(); 253 //pL = new PolylineClass(); 254 //pPointCollection = (IPointCollection)pL; 255 //pPoint = new PointClass(); 256 //pPoint.PutCoords(9.5, 6.5); 257 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 258 //pPoint.PutCoords(15.5, 6.5); 259 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 260 //pPolylinesFeature.Shape = pL; 261 //pPolylinesFeature.Store(); 262 263 //pPolylinesFeature = pPolylineClass.CreateFeature(); 264 //pL = new PolylineClass(); 265 //pPointCollection = (IPointCollection)pL; 266 //pPoint = new PointClass(); 267 //pPoint.PutCoords(9.5, 4.5); 268 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 269 //pPoint.PutCoords(15.5, 4.5); 270 //pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 271 //pPolylinesFeature.Shape = pL; 272 //pPolylinesFeature.Store(); 273 //#endregion 274 275 //// 像点 276 //#region 搞像点 277 //IFeatureClass pPointClass = pFeatureWorkspace.OpenFeatureClass("points"); 278 //IFeature pPointFeature = null; 279 280 //pPointFeature = pPointClass.CreateFeature(); 281 //pPoint = new PointClass(); 282 //pPoint.PutCoords(9.7, 9.5); 283 //pPointFeature.Shape = pPoint; 284 //pPointFeature.Store(); 285 286 //pPointFeature = pPointClass.CreateFeature(); 287 //pPoint = new PointClass(); 288 //pPoint.PutCoords(12.7, 9.5); 289 //pPointFeature.Shape = pPoint; 290 //pPointFeature.Store(); 291 292 //pPointFeature = pPointClass.CreateFeature(); 293 //pPoint = new PointClass(); 294 //pPoint.PutCoords(14.7, 9.5); 295 //pPointFeature.Shape = pPoint; 296 //pPointFeature.Store(); 297 298 //pPointFeature = pPointClass.CreateFeature(); 299 //pPoint = new PointClass(); 300 //pPoint.PutCoords(9.7, 6.5); 301 //pPointFeature.Shape = pPoint; 302 //pPointFeature.Store(); 303 304 //pPointFeature = pPointClass.CreateFeature(); 305 //pPoint = new PointClass(); 306 //pPoint.PutCoords(12.7, 6.5); 307 //pPointFeature.Shape = pPoint; 308 //pPointFeature.Store(); 309 310 //pPointFeature = pPointClass.CreateFeature(); 311 //pPoint = new PointClass(); 312 //pPoint.PutCoords(14.7, 6.5); 313 //pPointFeature.Shape = pPoint; 314 //pPointFeature.Store(); 315 316 //pPointFeature = pPointClass.CreateFeature(); 317 //pPoint = new PointClass(); 318 //pPoint.PutCoords(9.7, 4.5); 319 //pPointFeature.Shape = pPoint; 320 //pPointFeature.Store(); 321 322 //pPointFeature = pPointClass.CreateFeature(); 323 //pPoint = new PointClass(); 324 //pPoint.PutCoords(12.7, 4.5); 325 //pPointFeature.Shape = pPoint; 326 //pPointFeature.Store(); 327 328 //pPointFeature = pPointClass.CreateFeature(); 329 //pPoint = new PointClass(); 330 //pPoint.PutCoords(14.7, 4.5); 331 //pPointFeature.Shape = pPoint; 332 //pPointFeature.Store(); 333 334 //#endregion 335 336 //pWorkspaceEdit.StopEditOperation(); 337 //pWorkspaceEdit.StopEditing(true); 338 339 //pCommonPolygonClass = pPolygonsClass; 340 //pCommonPolylineClass = pPolylineClass; 341 //pCommonPointClass = pPointClass; 342 #endregion 343 344 } 345 346 private void button9_Click(object sender, EventArgs e) 347 { 348 349 350 IWorkspaceFactory pFeatureWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); 351 IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pFeatureWorkspaceFactory.OpenFromFile("D:/Work", 0); 352 IFields pFields = pCommonPolygonClass.Fields; 353 if (isFileExist == true) 354 { 355 IFeatureClass pPolygonsFeatureClassOld = pFeatureWorkspace.OpenFeatureClass("Newpolygons"); 356 ITable pTable = (ITable)pPolygonsFeatureClassOld; 357 if (pTable != null) 358 { 359 pTable.DeleteSearchedRows(null); 360 } 361 } 362 #region 摄区出库 363 364 IFeatureClass pPolygonsFeatureClass = pFeatureWorkspace.CreateFeatureClass("Newpolygons", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); 365 isFileExist = true; 366 IFeature pPolygonFeaure; 367 IFeature pConvertFeature; 368 //IFeatureCursor pPolygonCursor = pCommonPolygonClass.Search(null, true); 369 //IFeature pFeaure = pPolygonCursor.NextFeature(); 370 371 for (int i = 0; i < pArray.Count;i++ ) 372 { 373 pPolygonFeaure = pPolygonsFeatureClass.CreateFeature(); 374 pConvertFeature = (IFeature)pArray.get_Element(i); 375 pPolygonFeaure.Shape = pConvertFeature.Shape; 376 pPolygonFeaure.Store(); 377 378 } 379 #endregion 380 381 #region 航线出库 382 //pFields = pCommonPolylineClass.Fields; 383 //IFeatureClass pPolylineFeatureClass = pFeatureWorkspace.CreateFeatureClass("Newpolines", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); 384 //IFeature pPloylineFeature; 385 386 //IFeatureCursor pPolylineCursor = pCommonPolylineClass.Search(null, true); 387 //IFeature pFeature1 = pPolylineCursor.NextFeature(); 388 //while (pFeature1!= null) 389 //{ 390 // pPloylineFeature = pPolylineFeatureClass.CreateFeature(); 391 // pPloylineFeature.Shape = pFeature1.Shape; 392 // pPloylineFeature.Store(); 393 // pFeature1 = pPolylineCursor.NextFeature(); 394 395 //} 396 397 #endregion 398 399 #region 像点 400 //pFields = pCommonPointClass.Fields; 401 //IFeatureClass pPointsFeatureClass = pFeatureWorkspace.CreateFeatureClass("Newpoints", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); 402 //IFeature pPointFeaure; 403 404 //IFeatureCursor pPointCursor = pCommonPointClass.Search(null, true); 405 //IFeature pFeature2 = pPointCursor.NextFeature(); 406 407 //while(pFeature2!=null) 408 //{ 409 // pPointFeaure=pPointsFeatureClass.CreateFeature(); 410 // pPointFeaure.Shape = pFeature2.Shape; 411 // pPointFeaure.Store(); 412 // pFeature2 = pPointCursor.NextFeature(); 413 414 415 416 //} 417 418 419 #endregion 420 421 422 } 423 424 private void radioButton2_CheckedChanged(object sender, EventArgs e) 425 { 426 427 } 428 429 private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) 430 { 431 432 } 433 434 private void button1_Click(object sender, EventArgs e) 435 { 436 IFeatureLayer pPolygonLayer=new FeatureLayerClass(); 437 IFeatureLayer pPolylineLayer= new FeatureLayerClass(); 438 IFeatureLayer pPointLayer= new FeatureLayerClass(); 439 pPolygonLayer.FeatureClass=pCommonPolygonClass; 440 pPolylineLayer.FeatureClass=pCommonPolylineClass; 441 pPointLayer.FeatureClass=pCommonPointClass; 442 443 axMapControl1.AddLayer(pPolygonLayer); 444 axMapControl1.AddLayer(pPolylineLayer); 445 axMapControl1.AddLayer(pPointLayer); 446 447 448 449 450 451 } 452 453 private void axMapControl1_OnMouseDown_1(object sender, IMapControlEvents2_OnMouseDownEvent e) 454 { 455 if (e.button == 2) 456 { 457 ISpatialReference pSR = axMapControl1.Map.SpatialReference; 458 ILayer pLayer = axMapControl1.Map.get_Layer(0); 459 IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer; 460 int count = pFeatureLayer.FeatureClass.FeatureCount(null); 461 MessageBox.Show("投影名字是:" + pSR.Name + " 图层Feature个数是:" + count); 462 } 463 } 464 465 private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) 466 { 467 if (e.button == 2) 468 { 469 ISpatialReference pSR = axMapControl2.Map.SpatialReference; 470 ILayer pLayer = axMapControl2.Map.get_Layer(0); 471 IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer; 472 int count = pFeatureLayer.FeatureClass.FeatureCount(null); 473 MessageBox.Show("投影名字是:" + pSR.Name + " 图层Feature个数是:" + count); 474 } 475 } 476 477 private void axMapControl3_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e) 478 { 479 if (e.button == 2) 480 { 481 ISpatialReference pSR = axMapControl3.Map.SpatialReference; 482 ILayer pLayer = axMapControl3.Map.get_Layer(0); 483 IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer; 484 int count = pFeatureLayer.FeatureClass.FeatureCount(null); 485 MessageBox.Show("投影名字是:" + pSR.Name + " 图层Feature个数是:" + count); 486 } 487 } 488 489 private void tabControl1_Selected(object sender, TabControlEventArgs e) 490 { 491 492 493 } 494 495 private void tabControl1_Click(object sender, EventArgs e) 496 { 497 498 499 } 500 501 private void axMapControl3_TabIndexChanged(object sender, EventArgs e) 502 { 503 504 505 506 } 507 508 //切换Tabs使用的方法 509 private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) 510 { 511 switch (tabControl1.SelectedIndex) 512 { 513 case 0: 514 axToolbarControl1.SetBuddyControl(axMapControl1); 515 Console.WriteLine("0"); 516 break; 517 case 1: 518 519 axToolbarControl1.SetBuddyControl(axMapControl2); 520 Console.WriteLine("1"); 521 break; 522 case 2: 523 axToolbarControl1.SetBuddyControl(axMapControl3); 524 Console.WriteLine("2"); 525 break; 526 default: 527 break; 528 } 529 530 } 531 532 private void button2_Click(object sender, EventArgs e) 533 { 534 IEnvelope pEnvelop = new EnvelopeClass(); 535 ILayer pLayer=axMapControl1.Map.get_Layer(0); 536 IFeatureLayer pFeatureLayer=(IFeatureLayer)pLayer; 537 IFeatureClass pFeatureClass=pFeatureLayer.FeatureClass; 538 IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false); 539 IFeature pFeature = pFeatureCursor.NextFeature(); 540 while (pFeature != null) 541 { 542 pEnvelop.Union(pFeature.Shape.Envelope); 543 pFeature = pFeatureCursor.NextFeature(); 544 } 545 pEnvelop.Expand(1.2, 1.2, true); 546 axMapControl1.ActiveView.Extent = pEnvelop.Envelope; 547 axMapControl1.Refresh(); 548 } 549 550 private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) 551 { 552 label2.Text = e.mapX.ToString(); 553 label3.Text = e.mapY.ToString(); 554 } 555 556 private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) 557 { 558 label2.Text = e.mapX.ToString(); 559 label3.Text = e.mapY.ToString(); 560 } 561 562 private void axMapControl3_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e) 563 { 564 label2.Text = e.mapX.ToString(); 565 label3.Text = e.mapY.ToString(); 566 } 567 568 private void button3_Click(object sender, EventArgs e) 569 { 570 ICommand command = new ControlsAddDataCommandClass(); 571 command.OnCreate(axMapControl1.Object); 572 command.OnClick(); 573 } 574 575 576 //新建类似西安80PCS 577 private IProjectedCoordinateSystem CreatePCSBasedOnXianan80(IGeographicCoordinateSystem pGCS, double falseEasting, double fasleNorthing, 578 double centralMeridian, double scaleFactor, double latitudeOfOrigion)// 579 { 580 ISpatialReferenceFactory2 pSpatialReferenceFactory2=new SpatialReferenceEnvironmentClass(); 581 IProjectionGEN pProjectionGEN=(IProjectionGEN)pSpatialReferenceFactory2.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger); 582 IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory2.CreateGeographicCoordinateSystem((int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980); 583 (pGeographicCoordinateSystem as ISpatialReference).SetDomain(-400, -400, 400, 400); 584 ILinearUnit pLinearUnit=(ILinearUnit)pSpatialReferenceFactory2.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter); 585 IParameter[] pParameters=pProjectionGEN.GetDefaultParameters(); 586 for( int i=0;i< pParameters.Length;i++) 587 { 588 IParameter currentParameter=pParameters[i]; 589 } 590 //修改parameters 591 IParameter falseEastingParameter = pParameters[0]; 592 falseEastingParameter.Value = falseEasting; 593 IParameter fasleNorthingParameter = pParameters[1]; 594 fasleNorthingParameter.Value = fasleNorthing; 595 IParameter centralMeridianParameter = pParameters[2]; 596 centralMeridianParameter.Value =centralMeridian; 597 IParameter scaleFactorParameter = pParameters[3]; 598 scaleFactorParameter.Value =scaleFactor; 599 IParameter latitudeOfOrigionParameter = pParameters[4]; 600 latitudeOfOrigionParameter.Value =latitudeOfOrigion; 601 IProjectedCoordinateSystemEdit pPCSE = new ProjectedCoordinateSystemClass(); 602 object name = "new Dom Project cooridinate system"; 603 object alias="nDPos"; 604 object abbreviation="NDPCS"; 605 object remarks = "most eastern province in China"; 606 object usage="when making maps of new Dom"; 607 object gcsobject = pGeographicCoordinateSystem as object; 608 object unitobject = pLinearUnit as object; 609 object projection = pProjectionGEN as object; 610 object parameters = pParameters as object; 611 612 pPCSE.Define(ref name,ref alias,ref abbreviation,ref remarks,ref usage,ref gcsobject,ref unitobject,ref projection,ref parameters); 613 IProjectedCoordinateSystem pPCS = (IProjectedCoordinateSystem)pPCSE; 614 { 615 (pPCS as ISpatialReferenceTolerance).XYTolerance = 0.000001; 616 pPCS.SetDomain(-10002100, -10002100, 900709923374.099, 900733802274.099); 617 618 } 619 return pPCS; 620 621 622 623 } 624 625 //新建类似北京54PCS 626 private IProjectedCoordinateSystem CreatePCSBasedOnBeijing54(IGeographicCoordinateSystem pGCS, double falseEasting, double fasleNorthing, 627 double centralMeridian, double scaleFactor, double latitudeOfOrigion)// 628 { 629 ISpatialReferenceFactory2 pSpatialReferenceFactory2 = new SpatialReferenceEnvironmentClass(); 630 IProjectionGEN pProjectionGEN = (IProjectionGEN)pSpatialReferenceFactory2.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger); 631 IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory2.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954); 632 (pGeographicCoordinateSystem as ISpatialReference).SetDomain(-400, -400, 400, 400); 633 ILinearUnit pLinearUnit = (ILinearUnit)pSpatialReferenceFactory2.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter); 634 IParameter[] pParameters = pProjectionGEN.GetDefaultParameters(); 635 for (int i = 0; i < pParameters.Length; i++) 636 { 637 IParameter currentParameter = pParameters[i]; 638 } 639 //修改parameters 640 IParameter falseEastingParameter = pParameters[0]; 641 falseEastingParameter.Value = falseEasting; 642 IParameter fasleNorthingParameter = pParameters[1]; 643 fasleNorthingParameter.Value = fasleNorthing; 644 IParameter centralMeridianParameter = pParameters[2]; 645 centralMeridianParameter.Value = centralMeridian; 646 IParameter scaleFactorParameter = pParameters[3]; 647 scaleFactorParameter.Value = scaleFactor; 648 IParameter latitudeOfOrigionParameter = pParameters[4]; 649 latitudeOfOrigionParameter.Value = latitudeOfOrigion; 650 IProjectedCoordinateSystemEdit pPCSE = new ProjectedCoordinateSystemClass(); 651 object name = "new Dom Project cooridinate system"; 652 object alias = "nDPos"; 653 object abbreviation = "NDPCS"; 654 object remarks = "most eastern province in China"; 655 object usage = "when making maps of new Dom"; 656 object gcsobject = pGeographicCoordinateSystem as object; 657 object unitobject = pLinearUnit as object; 658 object projection = pProjectionGEN as object; 659 object parameters = pParameters as object; 660 661 pPCSE.Define(ref name, ref alias, ref abbreviation, ref remarks, ref usage, ref gcsobject, ref unitobject, ref projection, ref parameters); 662 IProjectedCoordinateSystem pPCS = (IProjectedCoordinateSystem)pPCSE; 663 { 664 (pPCS as ISpatialReferenceTolerance).XYTolerance = 0.000001; 665 pPCS.SetDomain(-10002300, -10002300, 900734802174.099, 900709923174.099); 666 } 667 return pPCS; 668 669 670 671 } 672 673 674 //新建类似WGS1984PCS 675 private IProjectedCoordinateSystem CreatePCSBasedOnWGS1984(IGeographicCoordinateSystem pGCS, double falseEasting, double fasleNorthing, 676 double centralMeridian, double scaleFactor, double latitudeOfOrigion)// 677 { 678 ISpatialReferenceFactory2 pSpatialReferenceFactory2 = new SpatialReferenceEnvironmentClass(); 679 IProjectionGEN pProjectionGEN = (IProjectionGEN)pSpatialReferenceFactory2.CreateProjection((int)esriSRProjectionType.esriSRProjection_GaussKruger); 680 IGeographicCoordinateSystem pGeographicCoordinateSystem = pSpatialReferenceFactory2.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); 681 (pGeographicCoordinateSystem as ISpatialReference).SetDomain(-400, -400, 400, 400); 682 ILinearUnit pLinearUnit = (ILinearUnit)pSpatialReferenceFactory2.CreateUnit((int)esriSRUnitType.esriSRUnit_Meter); 683 IParameter[] pParameters = pProjectionGEN.GetDefaultParameters(); 684 for (int i = 0; i < pParameters.Length; i++) 685 { 686 IParameter currentParameter = pParameters[i]; 687 } 688 //修改parameters 689 IParameter falseEastingParameter = pParameters[0]; 690 falseEastingParameter.Value = falseEasting; 691 IParameter fasleNorthingParameter = pParameters[1]; 692 fasleNorthingParameter.Value = fasleNorthing; 693 IParameter centralMeridianParameter = pParameters[2]; 694 centralMeridianParameter.Value = centralMeridian; 695 IParameter scaleFactorParameter = pParameters[3]; 696 scaleFactorParameter.Value = scaleFactor; 697 IParameter latitudeOfOrigionParameter = pParameters[4]; 698 latitudeOfOrigionParameter.Value = latitudeOfOrigion; 699 IProjectedCoordinateSystemEdit pPCSE = new ProjectedCoordinateSystemClass(); 700 object name = "new Dom Project cooridinate system"; 701 object alias = "ndpos"; 702 object abbreviation = "NDPCS"; 703 object remarks = "most eastern province in China"; 704 object usage = "when making maps of new Dom"; 705 object gcsobject = pGeographicCoordinateSystem as object; 706 object unitobject = pLinearUnit as object; 707 object projection = pProjectionGEN as object; 708 object parameters = pParameters as object; 709 710 pPCSE.Define(ref name, ref alias, ref abbreviation, ref remarks, ref usage, ref gcsobject, ref unitobject, ref projection, ref parameters); 711 IProjectedCoordinateSystem pPCS = (IProjectedCoordinateSystem)pPCSE; 712 { 713 (pPCS as ISpatialReferenceTolerance).XYTolerance = 0.000001; 714 pPCS.SetDomain(-9998100, -9998100, 900714804574.099, 900709927374.099); 715 716 } 717 return pPCS; 718 } 719 720 721 private void button1_Click_1(object sender, EventArgs e) 722 { 723 ITable pTable=pPublicFeatureWorkspace.OpenTable("DomDataTest"); 724 int indexofArea=pTable.Fields.FindField("AreaDefinition2"); 725 int indexofGCS = pTable.Fields.FindField("Globle"); 726 int indexoffalseEasting = pTable.Fields.FindField("falseEasting"); 727 int indexoffalseNorthing = pTable.Fields.FindField("falseNorthing"); 728 int indexofcentralMeridian = pTable.Fields.FindField("centralMeridian"); 729 int indexofscaleFactor = pTable.Fields.FindField("scaleFactor"); 730 int indexoflatitudeOfOrigion = pTable.Fields.FindField("LatitudeOfOrigion"); 731 ICursor pCursor=pTable.Search(null,false); 732 IRow pRow=pCursor.NextRow(); 733 string StrXY=string.Empty; 734 string StrGCS = string.Empty; 735 while(pRow!=null) 736 { 737 StrGCS = (string)pRow.get_Value(indexofGCS); 738 //如果pRow的“数据坐标系”是北京54 739 switch ( StrGCS ) 740 { 741 case "北京54": 742 743 Globle = "北京54"; 744 StrXY = (string)pRow.get_Value(indexofArea); 745 XY = StrXY; 746 falseEasting = (double)pRow.get_Value(indexoffalseEasting); 747 falseNorthing = (double)pRow.get_Value(indexoffalseNorthing); 748 centralMeridian=(double) pRow.get_Value(indexofcentralMeridian); 749 scaleFactor = (double)pRow.get_Value(indexofscaleFactor); 750 latitudeOfOrigion = (double)pRow.get_Value(indexoflatitudeOfOrigion); 751 752 break; 753 case "西安80": 754 StrXY = (string)pRow.get_Value(indexofArea); 755 XY = StrXY; 756 757 break; 758 case "WGS84": 759 StrXY = (string)pRow.get_Value(indexofArea); 760 XY=StrXY; 761 762 break; 763 default: 764 break; 765 766 } 767 //如果pRow的“数据坐标系”是西安80 768 769 770 771 772 //如果pRow的“数据坐标系”是WGS84 773 pRow = pCursor.NextRow(); 774 775 } 776 777 778 779 780 } 781 782 //解析字符串 783 private string[] ParseAreaDefinition2(string AreaStr) 784 { 785 int begin; 786 int end; 787 string[] split2=null; 788 string[] split1; 789 string sub = string.Empty; 790 791 split1 = AreaStr.Split(new char[] { '+' }); 792 foreach (string a in split1) 793 { 794 if (a.Trim() != "") 795 { 796 begin = a.IndexOf('('); 797 end = a.IndexOf(')'); 798 sub = a.Substring(begin + 1, end - begin - 1); 799 split2 = sub.Split(new char[] { ';' }); 800 } 801 802 } 803 return split2; 804 805 } 806 807 808 809 //Create inMemoryWorkspace 810 private IWorkspace CreateInMemoryWorkspace() 811 { 812 // Create an InMemory workspace factory. 813 IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); 814 // Create an InMemory geodatabase. 815 IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", 816 null, 0); 817 // Cast for IName. 818 IName name = (IName)workspaceName; 819 //Open a reference to the InMemory workspace through the name object. 820 IWorkspace workspace = (IWorkspace)name.Open(); 821 return workspace; 822 } 823 824 825 private void button2_Click_1(object sender, EventArgs e) 826 { 827 828 IWorkspace pInMemoryWorkspace = CreateInMemoryWorkspace(); 829 IFeatureWorkspace pInMemoryFeatureWorkspace = (IFeatureWorkspace)pInMemoryWorkspace; 830 //----------------------------------------------------------------------------- 831 832 IFields pFields = new FieldsClass(); 833 IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; 834 835 IGeometryDef pGometryDef = new GeometryDefClass(); 836 IGeometryDefEdit pGeometryDefEdit = (IGeometryDefEdit)pGometryDef; 837 { 838 pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; 839 pGeometryDefEdit.GridCount_2 = 1; 840 pGeometryDefEdit.AvgNumPoints_2 = 2; 841 pGeometryDefEdit.HasM_2 = false; 842 pGeometryDefEdit.HasZ_2 = false; 843 pGeometryDefEdit.set_GridSize(0, 10); 844 pGeometryDefEdit.SpatialReference_2 = (ISpatialReference)CreatePCSBasedOnBeijing54((IGeographicCoordinateSystem)GetGCSBeijing54(), falseEasting, falseNorthing, 845 centralMeridian, scaleFactor, latitudeOfOrigion); 846 } 847 IField pField = new FieldClass(); 848 IFieldEdit pFieldEdit = (IFieldEdit)pField; 849 850 pFieldEdit.Name_2 = "Globle"; 851 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; 852 pFieldEdit.Length_2 = 50; 853 pFieldsEdit.AddField(pField); 854 855 pField = new FieldClass(); 856 pFieldEdit = (IFieldEdit)pField; 857 pFieldEdit.Name_2 = "Shape"; 858 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; 859 pFieldEdit.GeometryDef_2 = pGometryDef; 860 pFieldsEdit.AddField(pField); 861 862 pField = new FieldClass(); 863 pFieldEdit = (IFieldEdit)pField; 864 pFieldEdit.Name_2 = "OBJECTID"; 865 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; 866 pFieldsEdit.AddField(pField); 867 868 IFeatureClass pInMemoryFeatureClass = pInMemoryFeatureWorkspace.CreateFeatureClass("InMemory", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); 869 870 //-------------------------------------------------------------------------------- 871 872 IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pInMemoryFeatureWorkspace; 873 874 pWorkspaceEdit.StartEditing(true); 875 pWorkspaceEdit.StartEditOperation(); 876 877 878 //设置几何字段 879 880 string[] strXY = ParseAreaDefinition2(XY); 881 882 IPolygon pPolygon = new PolygonClass(); 883 IPointCollection pPointCollection = (IPointCollection)pPolygon; 884 IPoint pPoint; 885 object _missing = Type.Missing; 886 double x, y; 887 for (int i = 0; i < strXY.Length; i++) 888 { 889 XandY = strXY[i].Split(new char[] { ',', ',' }); 890 x = double.Parse(XandY[0]); 891 y = double.Parse(XandY[1]); 892 pPoint = new PointClass(); 893 pPoint.PutCoords(x, y); 894 pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 895 } 896 pPolygon.Close(); 897 898 //------------------------------------------------------------------------ 899 900 IFeatureBuffer pFeatureBuffer = pInMemoryFeatureClass.CreateFeatureBuffer(); 901 IFeatureCursor pFeatureCursor = pInMemoryFeatureClass.Insert(true); 902 903 int index; 904 { 905 try 906 { 907 pFeatureBuffer.Shape = pPolygon; 908 index = pInMemoryFeatureClass.Fields.FindField("Globle"); 909 pFeatureBuffer.set_Value(index, Globle); 910 } 911 catch (Exception ex) 912 { 913 MessageBox.Show(ex.Message); 914 } 915 } 916 pFeatureCursor.InsertFeature(pFeatureBuffer); 917 pFeatureCursor.Flush(); 918 //----------------------------------------------------------------------- 919 pPublicInMemoryClass = pInMemoryFeatureClass; 920 IFeatureLayer pFL = new FeatureLayerClass(); 921 pFL.FeatureClass = pInMemoryFeatureClass; 922 axMapControl1.AddLayer(pFL); 923 924 //------------------------------------------------------------------------ 925 926 } 927 928 929 //复制字段,包括几何字段、非几何字段和空间参考的设置 930 private IFields CloneFeatureClassFields(IFeatureClass pFeatureClass, ISpatialReference pSpatialReference) 931 { 932 IFields pInputFields = pFeatureClass.Fields; 933 IFields pOutputFields = null; 934 IField pShapeField = null; 935 IGeometryDefEdit pSFGeoDefEdit = null; 936 IFieldChecker pFieldChecker = new FieldCheckerClass(); 937 IEnumFieldError obj = null; 938 pFieldChecker.Validate(pInputFields, out obj, out pOutputFields); 939 pShapeField = pOutputFields.get_Field(pOutputFields.FindField(pFeatureClass.ShapeFieldName)); 940 pSFGeoDefEdit = (IGeometryDefEdit)pShapeField.GeometryDef; 941 pSFGeoDefEdit.SpatialReference_2 = pSpatialReference; 942 return pOutputFields; 943 944 } 945 //创建内存FeatureClass 946 private IFeatureClass CreateInMemoryFeatureClass(string name, IFields pInputFields) 947 { 948 IWorkspaceFactory pInMemoryWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); 949 IWorkspaceName pWorkspaceName = pInMemoryWorkspaceFactory.Create("", "InMemoryWorkspace", null, 0); 950 IName pName = (IName)pWorkspaceName; 951 IWorkspace pInMemoryWorkspace = (IWorkspace)pName.Open(); 952 IFeatureWorkspace pInMemoryFeatureWorkspace = (IFeatureWorkspace)pInMemoryWorkspace; 953 IFeatureClass pInMemoryFeatureClass = pInMemoryFeatureWorkspace.CreateFeatureClass(name, pInputFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); 954 return pInMemoryFeatureClass; 955 956 } 957 958 private void button3_Click_1(object sender, EventArgs e) 959 { 960 IFeatureClass pFC = pPublicInMemoryClass; 961 962 IFeatureClass pInsertFC = pPublicFeatureWorkspace.OpenFeatureClass("InsertDomBeijing54"); 963 964 { } 965 966 967 968 } 969 970 private void button4_Click(object sender, EventArgs e) 971 { 972 973 } 974 975 //非常重要 976 private void button4_Click_1(object sender, EventArgs e) 977 { 978 979 string[] strXY = ParseAreaDefinition2(XY); 980 981 IPolygon pPolygon = new PolygonClass(); 982 pPolygon.SpatialReference = (ISpatialReference)CreatePCSBasedOnBeijing54((IGeographicCoordinateSystem)GetGCSBeijing54(), falseEasting, falseNorthing, 983 centralMeridian, scaleFactor, latitudeOfOrigion); 984 IPointCollection pPointCollection = (IPointCollection)pPolygon; 985 IPoint pPoint; 986 object _missing = Type.Missing; 987 double x, y; 988 for (int i = 0; i < strXY.Length; i++) 989 { 990 XandY = strXY[i].Split(new char[] { ',', ',' }); 991 x = double.Parse(XandY[0]); 992 y = double.Parse(XandY[1]); 993 pPoint = new PointClass(); 994 pPoint.PutCoords(x, y); 995 pPointCollection.AddPoint(pPoint, ref _missing, ref _missing); 996 } 997 pPolygon.Close(); 998 pPolygon.Project(GetPCSBeijing54()); 999 1000 1001 IFeatureClass pInsertFC = pPublicFeatureWorkspace.OpenFeatureClass("InsertDomBeijing54");1002 IFeatureBuffer pInsertFB = pInsertFC.CreateFeatureBuffer();1003 IFeatureCursor pInsertFCursor = pInsertFC.Insert(true);1004 {1005 pInsertFB.Shape = pPolygon;1006 1007 }1008 pInsertFCursor.InsertFeature(pInsertFB);1009 pInsertFCursor.Flush();1010 1011 1012 }1013 //解析字符串1014 1015 1016 1017 1018 1019 1020 }1021 }
既然是别人的研究成果 先转来 看看把。