sicon2003
路人甲
路人甲
  • 注册日期2004-11-26
  • 发帖数12
  • QQ
  • 铜币106枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:2439回复:6

点坐标的提取????

楼主#
更多 发布于:2005-01-26 21:13
Dim dblX As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblX = pPoint.X
我按照这种方法提取点的坐标,但是却得出的不是经纬度坐标,而是页面坐标,,谁能告诉我怎样得到点的经纬度坐标呀
喜欢0 评分0
gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15951
  • QQ
  • 铜币25345枚
  • 威望15368点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
1楼#
发布于:2005-01-26 23:32
<H4>看看这个,应该会有收获</H4>
<H4> </H4>
<H4>Summary</H4>
<DIV>Instructions provided describe how to use ArcObjects to get the x and y coordinates of points, polygon centroids, or line midpoints.

<b>Point:</b> If a point layer is selected, the fields added will be called X_COORD and Y_COORD and will be populated with the feature's x and y coordinates.

<b>Line:</b> If a line layer is selected, the fields added will be called MID_X and MID_Y and will contain the coordinates of the midpoint of each line.

<b>Polygon:</b> If a polygon layer is selected, the fields added will be called CENTROID_X and CENTROID_Y and will contain the coordinates of the centroid of each polygon.

</DIV>

<H4>Procedure</H4>
<DIV>The ArcObjects code below adds two new fields to a feature layer and can be applied with a point, line, or polygon layer selection.


<OL>
<LI>Start ArcMap.
<LI>Add the feature class for which you want to calculate the coordinates. You must have write-access to the feature class.
<LI>Open the Visual Basic Editor. <a><NOBR><FONT color=#899c75>-show me-</FONT></NOBR></A>
<DIV>

<TABLE cellSpacing=0 cellPadding=2 width="75%" align=center border=0>

<TR>
<TD class=tdWhite>
<DIV>In ArcMap, select Tools > Macros > Visual Basic Editor.</DIV></TD></TR>
<TR>
<TD class=tdWhite>
<DIV></DIV></TD></TR></TABLE></DIV>
<LI>In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument then right-click and select View Code. <a><NOBR><FONT color=#899c75>-show me-</FONT></NOBR></A>
<DIV>

<TABLE cellSpacing=0 cellPadding=2 width="75%" align=center border=0>

<TR>
<TD class=tdWhite>
<DIV></DIV></TD></TR>
<TR>
<TD class=tdWhite>
<DIV><IMG src="http://support.esri.com/knowledgebase/techarticles/ShowImage.asp?ImageId=20698;ImageType=GIF"></DIV></TD></TR></TABLE></DIV>


<DIV ><IMG src="http://support.esri.com/common/style/graphics/icons/note.gif"> Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents open the Normal.mxt ThisDocument code module instead.</DIV>

<LI>Copy the following code into the code module.

<DIV ><PRE >Dim X_Pos As Integer

Dim Y_Pos As Integer

Dim fClass As IFeatureClass





Public Sub addCoords()

 ' Adds coordinate information to shapefile, coverage,

 ' and geodatabase feature class tables

 Dim mxDoc As IMxDocument

 Dim selectedLayer As ILayer

 Dim fLayer As IFeatureLayer

 

 Set mxDoc = ThisDocument

 Set selectedLayer = mxDoc.selectedLayer

 If selectedLayer Is Nothing Then

  MsgBox "Select a layer in the Table of Contents.", _

         vbCritical, "No layer selected"

  Exit Sub

 End If

 If Not TypeOf selectedLayer Is IFeatureLayer Then

  MsgBox "Select a feature layer in the Table of Contents.", _

         vbCritical, "Selected layer is not a feature layer"

  Exit Sub

 End If

 Set fLayer = selectedLayer

 Set fClass = fLayer.FeatureClass

 Select Case fClass.ShapeType

  Case esriGeometryPoint

   If AddFields("X_Coord", "Y_Coord") = True Then

    AddXY_Points

   End If

  Case esriGeometryPolyline

   If AddFields("Mid_X", "Mid_Y") = True Then

    AddXY_Lines  ' based on fromPoint of line.

   End If

  Case esriGeometryPolygon

   If AddFields("Centroid_X", "Centroid_Y") = True Then

    AddXY_Polygons  ' based on centroid of polygon.

   End If

  Case Else

   MsgBox "This tool only works with point, line and polygons.", _

          vbCritical, "Unsupported geometry type"

   Exit Sub

 End Select

End Sub



Private Function AddFields(fieldName1 As String, _

       fieldName2 As String) As Boolean

      

 ' Add required fields that will store the coordinate information.

 On Error GoTo couldntAddFields

 Dim newField As IFieldEdit

 

 ' Delete required field if it exists.

 If fClass.FindField(fieldName1) <> -1 Then

  fClass.DeleteField fClass.Fields.Field(fClass.FindField(fieldName1))

 End If

 

 ' Create the required field.

 Set newField = New Field

 With newField

  .Name = fieldName1

  .Type = esriFieldTypeDouble

 End With

 fClass.AddField newField

 

 ' Delete required field if it exists.

 If fClass.FindField(fieldName2) <> -1 Then

  fClass.DeleteField fClass.Fields.Field(fClass.FindField(fieldName2))

 End If

 

 ' Create the required field.

 Set newField = New Field

 With newField

  .Name = fieldName2

  .Type = esriFieldTypeDouble

 End With

 fClass.AddField newField

 X_Pos = fClass.FindField(fieldName1)

 Y_Pos = fClass.FindField(fieldName2)

 If X_Pos = -1 Or Y_Pos = -1 Then

  GoTo couldntAddFields

 End If

 AddFields = True

 Exit Function

couldntAddFields:

 MsgBox Err.Description ; Chr(13) ; "AddXY was not completed.", _

        vbCritical, "Could not add required fields"

 AddFields = False

End Function



Private Sub AddXY_Points()



 ' Populate X_Coord and Y_Coord fields for each point.

 Dim fCursor As IFeatureCursor

 Dim aFeature As IFeature

 Dim thePoint As IPoint

 

 Set fCursor = fClass.Update(Nothing, False)

 Set aFeature = fCursor.NextFeature

 Do Until aFeature Is Nothing

  Set thePoint = aFeature.Shape

  aFeature.Value(X_Pos) = thePoint.X

  aFeature.Value(Y_Pos) = thePoint.Y

  fCursor.UpdateFeature aFeature

  Set aFeature = fCursor.NextFeature

 Loop

 MsgBox "Finished", vbInformation, "Add Coordinates"

End Sub



Private Sub AddXY_Lines()

 ' Populate Mid_X and Mid_Y fields with the mid-point XY for each line.

 Dim fCursor As IFeatureCursor

 Dim aFeature As IFeature

 Dim theCurve As ICurve

 Dim thePoint As IPoint

 

 Set thePoint = New Point

 Set fCursor = fClass.Update(Nothing, False)

 Set aFeature = fCursor.NextFeature

 Do Until aFeature Is Nothing

  Set theCurve = aFeature.Shape

  theCurve.QueryPoint 0, 0.5, True, thePoint

  aFeature.Value(X_Pos) = thePoint.X

  aFeature.Value(Y_Pos) = thePoint.Y

  fCursor.UpdateFeature aFeature

  Set aFeature = fCursor.NextFeature

 Loop

 MsgBox "Finished", vbInformation, "Add Coordinates"

End Sub



Private Sub AddXY_Polygons()

 ' Populate Centroid_X and Centroid_X fields

 ' with the centroid for each polygon.

 Dim fCursor As IFeatureCursor

 Dim aFeature As IFeature

 Dim thePolygon As IArea

 Dim thePoint As IPoint

 

 Set fCursor = fClass.Update(Nothing, False)

 Set aFeature = fCursor.NextFeature

 Do Until aFeature Is Nothing

  Set thePolygon = aFeature.Shape

  Set thePoint = thePolygon.Centroid

  aFeature.Value(X_Pos) = thePoint.X

  aFeature.Value(Y_Pos) = thePoint.Y

  fCursor.UpdateFeature aFeature

  Set aFeature = fCursor.NextFeature

 Loop

 MsgBox "Finished", vbInformation, "Add Coordinates"

End Sub</PRE></DIV>

<LI>Close the Visual Basic Editor.
<LI>Run the code by clicking Tools > Macros > Macros, then change the 'Macros in:' to "Normal (Normal.mxt)", select "ThisDocument.addCoords" in the 'Macro name:' box, and then click Run. A message box will tell you when the code is finished running. </LI></OL></DIV>
[此贴子已经被作者于2005-1-26 23:37:27编辑过]
GIS麦田守望者,期待与您交流。
举报 回复(0) 喜欢(0)     评分
gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15951
  • QQ
  • 铜币25345枚
  • 威望15368点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
2楼#
发布于:2005-01-27 13:39
楼上说的是对的
GIS麦田守望者,期待与您交流。
举报 回复(0) 喜欢(0)     评分
sicon2003
路人甲
路人甲
  • 注册日期2004-11-26
  • 发帖数12
  • QQ
  • 铜币106枚
  • 威望0点
  • 贡献值0点
  • 银元0个
3楼#
发布于:2005-01-27 20:02
<P>谢谢大家,可是数据本身的坐标我当时并没有定义呀,我只是按照一个已经有经纬度坐标的shipfile文件进行配准的,现在我急切的想知道经纬度坐标,怎么解决呀</P>
举报 回复(0) 喜欢(0)     评分
sicon2003
路人甲
路人甲
  • 注册日期2004-11-26
  • 发帖数12
  • QQ
  • 铜币106枚
  • 威望0点
  • 贡献值0点
  • 银元0个
4楼#
发布于:2005-01-27 20:35
<P>我得到的是gengral 里米显示时的坐标,可是我换成经纬度显示,页面上是有了经纬度信息,,但是我却提取不出来,坐标提取出的还是米显示时的坐标,,,,,,,,,,</P>
举报 回复(0) 喜欢(0)     评分
gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15951
  • QQ
  • 铜币25345枚
  • 威望15368点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
5楼#
发布于:2005-01-28 22:05
<P>你需要在arctoolbox里进行坐标系和投影转换才有用,你直接在arcmap里改显示单位,数据是没有得到改变的</P>
GIS麦田守望者,期待与您交流。
举报 回复(0) 喜欢(0)     评分
wavvylia
路人甲
路人甲
  • 注册日期2003-07-28
  • 发帖数384
  • QQ
  • 铜币555枚
  • 威望0点
  • 贡献值0点
  • 银元0个
6楼#
发布于:2005-01-31 11:23
<P>这说明你的所加的坐标系不是地理坐标。显示成米不见得他就不是地理坐标系。</P>
举报 回复(0) 喜欢(0)     评分
游客

返回顶部