Wednesday 1 November 2017

Determining Y coordinate for Create Fishnet from desired Angle of Rotation using ArcPy/Python?


In How Create Fishnet works from the ArcGIS Help it says:


Calculating a value for the y-axis coordinate
If you know the angle of rotation, you can compute a value for the Y-Axis Coordinate parameter as follows:

Tangent of angle = x-coordinate / y-coordinate

Fishnet Y-Axis point calculation

For example, the angle is 60 degrees. Assuming the y-coordinate to be 10, then

x-coordinate = tan(60) * 10 = 1.732 * 10 = 17.32

The y-axis coordinate point is (17.32,10).

In Python.


x-coordinate = (math.tan(math.radians(60) * 10) * 10)
print x-coordinate
>>>17.32


Perfect that's correct!


Now lets try using a real coordinate


Lat = 51.003757 Long, -114.09341083433694
or in NAD83 UTM Zone 11
x, y = 703919.581359, 5654264.1538 in UTM Zone 11

Using the same formula replacing 10 with the y-coordinate of the lat/long, and x,y of UTM as above none of the results make sense. What am I missing?



Answer



import arcpy, traceback, os, sys, math

from math import radians,sin,cos
from arcpy import env
env.overwriteOutput = True
inFC=arcpy.GetParameterAsText(0)
outFolder=arcpy.GetParameterAsText(1)
nRows,nCols=4,3
env.workspace = outFolder
rectangle=r'in_memory\rectangle'
tempf=r'in_memory\many'
outFile="fnet.shp"

## ERROR HANDLING
def showPyMessage():
arcpy.AddMessage(str(time.ctime()) + " - " + message)
def ShapeMake(pGon,angle):
ar=arcpy.Array()
a=radians(angle)
part=pGon.getPart(0)
for p in part:
x,y=p.X,p.Y
xN=cos(a)*x+sin(a)*y

yN=-sin(a)*x+cos(a)*y
pN=arcpy.Point(xN,yN)
s='%s %s' %(xN,yN)
ar.add(pN)
pgonRotated=arcpy.Polygon(ar)
return pgonRotated
try:
arcpy.MinimumBoundingGeometry_management(inFC,rectangle,
"RECTANGLE_BY_WIDTH", "ALL", "", "MBG_FIELDS")
with arcpy.da.SearchCursor(rectangle, ("SHAPE@","MBG_Orientation")) as rows:

for row in rows:
shp,angle = row
del row,rows
onside=ShapeMake(shp,-angle)
extent=onside.extent
origPoint='%s %s' %(extent.XMin,extent.YMin)
yPoint='%s %s' %(extent.XMin,extent.YMax)
endPoint='%s %s' %(extent.XMax,extent.YMax)
arcpy.CreateFishnet_management(tempf, origPoint,yPoint,
"0", "0", nRows, nCols,endPoint,

"NO_LABELS", "", "POLYGON")
with arcpy.da.UpdateCursor(tempf, "SHAPE@") as rows:
for row in rows:
shp = row[0]
rollBack=ShapeMake(shp,angle)
row[0]=rollBack
rows.updateRow(row)
del row,rows
arcpy.CopyFeatures_management(tempf, outFolder+os.sep+outFile)
except:

message = "\n*** PYTHON ERRORS *** "; showPyMessage()
message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"; showPyMessage()

this is example of output enter image description here


Yes it is more than 2 lines of code


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...