Select newly created feature

1462
13
Jump to solution
03-14-2014 07:04 AM
TonyAlmeida
MVP Regular Contributor
I have a script that creates a point with a mouse click (onMouseDownMap). The feature class is in a SDE geodatabase which has about 80,000 features in it. An edit session and an edit operation is needed, creating the point work just fine but i need some code after stopping the edit session to select the point that was just created to be able to pass to another function. The problem with starting the an edit session and an edit operation is that it clears the selection.

Any help would be awesome!
Thanks.
Tags (2)
0 Kudos
13 Replies
TonyAlmeida
MVP Regular Contributor
This is a new script that i am trying to build and get working.

I am not sure if it's working or not because when i run it the script that i posted above ArcMap just sits there thinking and thinking, i eventually have to use task manager to close arc map. I believe my problem is that the script takes every point and every taxparcels in the spatial join instead of just the newly point and the taxparcel it sits on. but i am not 100% certain becasue my python is not very good.

what i have been trying to is do a select by location on the parcel layer to see what parcel the new point sits on then populate/copy the point with certain fields from that selected parcel only.

I have separated the two functions and they run great separated but not together.

This function of this script runs flawless by it's self and as long as the point is selected. except for the "OBJECTID" does not get populated.
I create points in arcmap manually with the create features/construction tool then select them with the selection tool and run the following the points get populated with the parcel info it sits on only.
fcTarget = "Points_3"
workspace = r"C:\Temp\default.gdb"
#workspace = r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection to dsd15_sqlexpress.sde"
arcpy.env.overwriteOutput = True

####Select by location on parcels with created point
Parcellyr = "testParcelsAdmit"

arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer")
entries = int(arcpy.GetCount_management(fcTarget).getOutput(0))

for i in xrange(entries):
    arcpy.MakeFeatureLayer_management(fcTarget, "point layer", "\"OBJECTID\"={}".format(str(i)))
    arcpy.SelectLayerByLocation_management("Parcel layer", "INTERSECT", fcTarget, "", "NEW_SELECTION")
    #if arcpy.Exists(pt_lyr): arcpy.Delete_management(pt_lyr)

#### populates fields

add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","SiteStreet","predir","StreetType","SubName"]

# fix args
if not isinstance(add_fields, list):
    # from script tool
    add_fields = add_fields.split(';')

# do not need this scratch file
fcOutput = r'in_memory\temp_join'
arcpy.SpatialJoin_analysis("Parcel layer", fcTarget, fcOutput, 'JOIN_ONE_TO_MANY', 'KEEP_COMMON')


# grab oid field from points
oid_t = arcpy.Describe(fcTarget).OIDFieldName

# init rowW and rowR
curR = arcpy.SearchCursor(fcOutput)
join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR])
del curR

# Now update the new target
curW = arcpy.UpdateCursor(fcTarget)
for row in curW:
    t_oid = row.getValue(oid_t)
    if t_oid in join_dict:
        for f in add_fields:
            row.setValue(f, join_dict[t_oid][add_fields.index(f)])
    curW.updateRow(row)
del row, curW
arcpy.Delete_management(r"in_memory\temp_join")
arcpy.AddMessage('Updated all records sucussefully')    


Would some sample data help?
0 Kudos
JamesCrandall
MVP Alum
This is a new script that i am trying to build and get working.

I am not sure if it's working or not because when i run it the script that i posted above ArcMap just sits there thinking and thinking, i eventually have to use task manager to close arc map. I believe my problem is that the script takes every point and every taxparcels in the spatial join instead of just the newly point and the taxparcel it sits on. but i am not 100% certain becasue my python is not very good.

what i have been trying to is do a select by location on the parcel layer to see what parcel the new point sits on then populate/copy the point with certain fields from that selected parcel only.



As an alternative to a SelectByLocation after your point creation process, why not collect the Parcel attributes when you first click the location to create the point?  Hold them in variables to populate the attributes of the new point feature you create later.  This would alleviate the need to perform a lengthy SelectByLocation process (if that is what is causing your performance hit, my guess this is the culprit if you have 10's of thousands of parcels).
0 Kudos
TonyAlmeida
MVP Regular Contributor
jamesfreddyc that sounds intresting, but how would pass the click (input) to do the selection by location on the parcels?
thanks for the replay.
0 Kudos
JamesCrandall
MVP Alum
jamesfreddyc that sounds intresting, but how would pass the click (input) to do the selection by location on the parcels?
thanks for the replay.


See this thread for some examples: http://gis.stackexchange.com/questions/49713/select-and-copy-features-in-arcmap-using-python-add-in-...

Important part is:

  def onMouseDownMap(self, x, y, button, shift):     mxd = arcpy.mapping.MapDocument("CURRENT")     pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference)     searchdistance = getSearchDistanceInches(mxd.activeDataFrame.scale)     lyr = arcpy.mapping.ListLayers(mxd)[0] # assumes you want to select features from 1st layer in TOC     arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", pointGeom, "%d INCHES" % searchdistance)     arcpy.RefreshActiveView() 


Make sure "lyr" is referencing your Parcel layer, then after the refresh you should be able get access to the attributes with a SearchCursor.  Use the values in the cursor to populate your point feature with.

(note: I have not implemented the code above, so you would have validate it works.)
0 Kudos