How can I use geometry method snapToLine to find new coordinates?

4187
15
Jump to solution
02-08-2016 06:22 PM
RebeccaStrauch__GISP
MVP Emeritus

Using 10.3.1.

From Geometry—Help | ArcGIS for Desktop

I'm probably missing something basic here, but given a polyline, and a point geometry....how do I use snapToLine(in_point) and how do I capture the new point (in a python script)?

I've tested using arcpy.Near_analysis, which will work, but the description sounds more like what I need.  This is a small part of a bigger script.....which I'm now trying using a numpy array.  A general description, given my FC of classified points, I grab the first point...create the contour....snap the point to the contour and split at the point...then go a given distance in each direction along the line, split and create a transect...capture the coordinates at the three points. Previously I had this in Avenue and I'm updating, and adding new features, of course.  There's a bit more to it, but that's a general description for anyone interested.

Fwiw, I'll include my test script (I'll clean it up once I get this figured out...lots of stuff not being used in in this part.)

import os
import numpy as np
from numpy.lib import recfunctions as rfn  
import arcpy
from arcpy import env
from arcpy.sa import *
#from ADFGUtils import *
#from gpdecorators import *

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Set the Geoprocessing environment...
initialSR = arcpy.SpatialReference(3338)
geoSR = arcpy.SpatialReference(4269)

theWorkspace = r'C:\_beartest\Prep.gdb'
arcpy.env.workspace = theWorkspace
arcpy.env.overwriteOutput = True    
inStudy = r'C:\_beartest\Prep.gdb\Unit20A_boundary'
arcpy.env.Extent = inStudy

inDEM = r"C:\_beartest\Unit20Araster.gdb\elevExtent"  # clip later or \elevClip ..
rasterGDB, rasterFile = os.path.split(inDEM)
arcpy.env.snapRaster = inDEM
outContour = arcpy.os.path.join(theWorkspace, "c3")
outContour2 = arcpy.os.path.join(theWorkspace, "c4")

inPts = r'C:\_beartest\Prep.gdb\Pts1_3338'   # arcpy.ListFeatureClasses("Pts*3338*", "Point")
minTrans = '5000 Meters'    
maxTrans = '20000 Meters'
length, unit = maxTrans.split(" ")
halfTrans = int(length) / 2
flatOption = "fishnet"    

nearSearchRadius = "1000 Meters"
location = "LOCATION"    
angle = "ANGLE"
tmpPt = arcpy.os.path.join(theWorkspace, "tmpPt")

tmpRoute = arcpy.os.path.join(theWorkspace, "tmpRoute")    

field_names = "*"
arr = arcpy.da.FeatureClassToNumPyArray(inPts, field_names, "", initialSR)
dtypes = arr.dtype
arr2 = (np.array(arr, dtype=dtypes)).view(np.recarray) 

cnt = 0
while  cnt < 10:
    print("{0} PtID {1} elev: {2}".format(cnt, arr2.PtID[cnt], arr2.elev_ft[cnt]))
    transtype = arr2.TransType[cnt]
    ptID = arr2.PtID[cnt]
    elevM = arr2.elev_m[cnt]
    origX = arr2.Shape[cnt][0]
    origY = arr2.Shape[cnt][0]
    if transtype == "Riparian":
        print("this is a Riparian")
    elif cnt == 9:
        print("this is a {0}".format(transtype))
        ContourList(inDEM, outContour, elevM) 
        ptGeom = arcpy.PointGeometry(arcpy.Point(origX, origY, 0, 0, ptID))
        #nearPt = arcpy.Near_analysis(ptGeom, outContour, nearSearchRadius, "LOCATION", "ANGLE")
        #newPt = outContour.snapToLine(ptGeom)
    cnt += 1

variables of interest:  ptGeom, outContour

Hopefully it is something I'm just overlooking.

0 Kudos
15 Replies
RebeccaStrauch__GISP
MVP Emeritus

Thanks for the script, I'll take a look at it a bit more.  I did have a  couple SearchCursor in one of my versions, but was rethinking it trying to use the numpy arrays.  

0 Kudos
DanPatterson_Retired
MVP Emeritus

I have the numpy version.... just can't find it yet, it is in my notes.  I worked on the searchcursor logic first, then speed things up using numpy.  I hope this is not mission critical but an incremental for interest need, since I am on a variety of other tangents.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Only mission critical for me, and I have enough to go on now.  Fresh day.....fresher mind.   Many ways to get any task done.  If you eventually write something up on your blog...that would be a good read.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I think I did see that one yesterday in my many searches.  What it will come down to is a combo of a few....and I'm starting to swing back the direction of using Near_analysis and then maybe Join Field.  But, keep having other things pop up, so a start and stop testing session.  The line I snap to will be created on the fly, (contour) which is why most of the semi- OTB (out of the box) solutions won't work.

I'll post what I end up with.  But thanks for the link.  It doesn't hurt for me to look at it again.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I'm marking Dan's response as the closest to the answer, since he had a script  that helped me figure it out.  I also found some of my older python scripts that I was doing something similar.

I find the description and examples in the Help lacking.  For me, sample code goes a long way.

Thanks for all the suggestions.

edit: btw, no code cleaned up enough to make if worth posting yet.

0 Kudos