How to do simple geometry intersection

2419
6
08-16-2012 02:37 PM
JayDub
by
Emerging Contributor
I'm real new to arcpy and am trying to feel my way around. I have an application where the user supplies either a point/polygon in JSON, I convert that to an ESRI geometry and now I'm trying to find out if it intersects with a layer. I'm kind of lost.
The touches function provided on each geometry I'm thinking won't do because it seems touch only checks if a geom touches the border of another geom. Is this accurate?

Also it seems it can be done using Select_analysis? I saw a caution about it only working for Feature layers and not feature classes. Huh? What's the difference even.

I tried using Intersect_analysis. Not sure if this will make sense but here's the code.
def geom_intersects(esri_geom, layer):

 ts = int(time.time())
 intersect_tbl = "in_memory\\tmp_tbl_intersection_%s" % ts

 if arcpy.Exists(intersect_tbl):
  arcpy.management.Delete(intersect_tbl)

 rows = arcpy.SearchCursor(layer)
 for (index, row) in enumerate(rows):
  arcpy.analysis.Intersect([row.Shape, esri_geom], intersect_tbl + "_" + str(index))
  intersect_rows = arcpy.SearchCursor(intersect_tbl + "_" + str(index))
  if intersect_rows:
   for intersect_row in intersect_rows:
    print "Intersection here: ", intersect_row.Shape.area, intersect_row.Shape.length, intersect_row.Shape.type
   print " --- We have an intersection here: %s" % row.FID
   return True
 return False
Tags (2)
0 Kudos
6 Replies
JayDub
by
Emerging Contributor
Any ideas for this... anyone?
0 Kudos
ChristopherThompson
Frequent Contributor
I'm not precisely sure what you mean by creating esri geometry from the JSON - are you creating a shapefile? a geodatabase feature class? or an in-memory feature layer?  if  you are creating any of those three then you should be able to do a 'select by location' between that layer or features in it against your test layer.  Look at the help for arcpy.SelectByLocation_management to see if that helps.
0 Kudos
JayDub
by
Emerging Contributor
Sorry if I was unclear. My user is going to draw a polygon on the map or supply a point (dragging and dropping a push pin onto the map). Whichever geom is supplied (point or polygon) I want to convert that to JSON and send it to a script on my server. When the script on the server gets it I want to intersect that point/polygon with a layer that's stored on the server to find out if it intersects with the polygon/point.

At worst it'll probably be a couple of polygons or one point, so you're saying I should convert those polygons/point to at least an in-memory feature layer? Maybe a shapefile or geodatabase feature class may be overkill? 

I'm very new to ArcGIS Python so I'm not sure what approach I should take to getting this done. What's the best way to store these "layers" on the server? In a geodatabase?
0 Kudos
KevinYanuk
Deactivated User
Sorry if I was unclear. My user is going to draw a polygon on the map or supply a point (dragging and dropping a push pin onto the map). Whichever geom is supplied (point or polygon) I want to convert that to JSON and send it to a script on my server. When the script on the server gets it I want to intersect that point/polygon with a layer that's stored on the server to find out if it intersects with the polygon/point.

At worst it'll probably be a couple of polygons or one point, so you're saying I should convert those polygons/point to at least an in-memory feature layer? Maybe a shapefile or geodatabase feature class may be overkill? 

I'm very new to ArcGIS Python so I'm not sure what approach I should take to getting this done. What's the best way to store these "layers" on the server? In a geodatabase?



What exactly is your code above outputting?  A group of strings you are parsing to JSON?

What part of your code functions properly at the moment and what is not working?

As far as looking for intersection, if you don't want an output layer, shapefile, or featureclass, you can look into SelectLayerByLocation_management:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000072000000

Where you can then run an additional cursor through those 'selected' features.  If the cursor is empty, it has not intersected the source layer.
0 Kudos
JayDub
by
Emerging Contributor
Ok, here is what I'm doing...
My app allows the user to use the map to draw a polygon or upload a polygon shapefile. The script on the server needs to find out if the polygon(s) supplied by the user interact with the layers I have saved already.

I was thinking that the feature layers would be stored in ArcGIS server (10.1) as Geodatabase feature classes and also published as services to ArcGIS Server. I was thinking I would use arcpy code to access the geodatabase layers to do the querying and use the Service feature layers on the map to show the user the layer I'm comparing their geom to.

Is this the best way to go about doing this?
0 Kudos
JayDub
by
Emerging Contributor
Ok, here is what I'm doing...
My app allows the user to use the map to draw a polygon or upload a polygon shapefile. The script on the server needs to find out if the polygon(s) supplied by the user interact with the layers I have saved already.

I was thinking that the feature layers would be stored in ArcGIS server (10.1) as Geodatabase feature classes and also published as services to ArcGIS Server. I was thinking I would use arcpy code to access the geodatabase layers to do the querying and use the Service feature layers on the map to show the user the layer I'm comparing their geom to.

Is this the best way to go about doing this?


Hi, can anyone help me with this? I'm having serious trouble with this. The method I've done takes way too long. When the user uploads a shapefile I take the following steps:
- Open the shapefile and store the geoms in an array
- Make a feature layer for the comparison layer in ArcGIS server
- Use SelectLayerByLocation then run a SearchCursor to determine intersection

I have two main problems with this approach:
- It's horrendously slow... I mean, the whole process can take minutes... which is spectacularly frustrating...
- Sometimes arcpy.Describe throws an exception, claiming the file I JUST uploaded doesn't exist.
- Other times it just sits there until Python dies.

I'm currently deploying my solution using Python WSGI under WAMP. You guys have any suggestions? I'm desperate here and working with a past deadline. 😞
0 Kudos