Select to view content in your preferred language

Moving shape files based on attribute values programatically ?

1306
7
07-15-2012 04:50 AM
AdarshMadhavan
Occasional Contributor
Hi ESRI geeks,


How to approach the below problem programmatically ?

Have two sets of shape files.

One shape file is georeferenced and has got information of all administrative boundaries with certain code.

Another set is buildings whose name is same as a value in the attribute table of the parent boundaries they should be contained in, but Non-georeferenced.

Can I move these buildings to the boundaries based on the attribute table name ?


Please help me.

Regards,

intellgis
Tags (2)
0 Kudos
7 Replies
AdarshMadhavan
Occasional Contributor
Can I do something with update cursor ?
0 Kudos
MarcCusumano
Occasional Contributor
If it is OK to have the buildings stacked as a single point in the center of their respective administrative polygons, you could run this process:

1) Convert all administrative polygons to points using Feature to Point tool.
2) Calculate X / Y values on administrative boundary points.
3) Calculate new field in your buildings feature to equal feature name (administrative code) : see http://mappingcenter.esri.com/index.cfm?fa=ask.answers&q=1529 (9.3, you may need to research how to do this in 10)
3) Join your buildings feature with administrative boundaries feature based on administrative code.
4) Export Join, which will now have admin X / Y.
5) Right click the "SHAPE" field in your newly exported feature, select Python, click "Show Codeblock" and enter the following into the Pre-Logic Script Code:

def XYsetVALUE( shape, X_value, Y_value):
   point = shape.getPart(0)
   point.X = X_value
   point.Y = Y_value
   return point


Enter the following into the "Shape" box:

XYsetVALUE ( !SHAPE!, !X!, !Y! )


Click OK. Your building points will move to their respective administrative boundaries.
0 Kudos
AdarshMadhavan
Occasional Contributor
Dear MCusumano,

I appreciate the answer and solution - yet to check it though.

+1 for you 🙂

Intellgis
0 Kudos
AdarshMadhavan
Occasional Contributor
Dear MCusumano,

I nearly executed the program to perfection , but at the final stage, the Python Geoprocessing code wont work.

Possible Reason 1 -the centroid x and centroid y field names are POINT_X and POINT_Y,  different from the code (point.x amd point.y ).

Solution : tried to change the code to match the field names.

Possible reason 2 - The building shape files have no cordinate system defined. Shall I try to define projection to the same CRS of the administrative boundaries/centroids ???

Thanks for the timely help again.
0 Kudos
MarcCusumano
Occasional Contributor
Yes set the coordinate system of the buildings the same as the boundaries. To get the code to work, leave the Pre-logic script code the same and set the "Shape =" box to:

XYsetVALUE ( !SHAPE!, !POINT_X!, !POINT_Y! )
0 Kudos
AdarshMadhavan
Occasional Contributor
Dear MC,

Apologies for missing out a significant point on your post dated 07-16-2012 07:12 AM.

My buildings are not as points , but as Polygons.

I can either
1 - create centroids for these building polygons AS POINTS and use your code ( which is only partial fulfilment)

2- Or move the polygons programmatically to the Administrative Centroids.

If I am not demanding too much, I would like MCusumanos or other experts view on this.

Thanks to all again.
0 Kudos
AdarshMadhavan
Occasional Contributor
I tried a VB script given by Dean Thevaos in the below forum http://forums.esri.com/Thread.asp?c=93&f=992&t=213078

This is how you should do it. Make sure you are calculating the "Shape" field and you have a field named "XY" that contains the X and Y coordinates seperated by a comma.
 
Dim pPolygon As IPolygon
Dim pArea As IArea
Dim pOrigin As IPoint
Dim sXYField As String
Dim dXY() As String
Dim dX As Double
Dim dY As Double
Dim pTransform As ITransform2D

sXYField = [XY]

If (Not IsNull([Shape])) Then
  Set pPolygon = [Shape]
  dXY = Split(sXYField, ",")
  dX = CDbl(dXY(0))
  dY = CDbl(dXY(1))

  If (Not pPolygon.IsEmpty) Then
    Set pArea = pPolygon
    Set pOrigin = pArea.Centroid
    Set pTransform = pPolygon
    dX = dX - pOrigin.X
    dY = dY - pOrigin.Y
    pTransform.Move dX, dY
  End If
End If

__esri_field_calculator_splitter__

pPolygon

This also giving me errors ! but a syntax error may be I will be able to solve
0 Kudos