Help with Python Script - Selecting by Location - Sql string

2557
3
Jump to solution
07-23-2013 05:36 AM
DeleteDelete
New Contributor
My goal with this script is to select points within a polygon based on a sql statement that identifies the polygon to base the extraction on.  If anyone has any ideas, it would be greatly appreciated.



# Import arcpy module
import arcpy


# Local variables:
SQL_Expression = "SELECT * FROM Public_Seed_Grounds WHERE \"SEEDGRND\" = 'Lake Felicity'"
Public_Seed_Grounds_shp = "C:\\Path\\Public_Seed_Grounds.shp"
VMS_InsideSDG = "VMS_InsideSDG"
Lake_Felicity = "VMS_InsideSDG"
FelicityVMS_PointsforAnalysis100_shp = "C:\\Path\\FelicityVMS_PointsforAnalysis100.shp"
FelicityVMS_PointsforAnalysis100_Collect_shp = "C:\\Path\\FelicityVMS_PointsforAnalysis100_Collect.shp"
FelicityVMS_PointsforAnalysi_shp = "C:\\Path\\FelicityVMS_PointsforAnalysi.shp"
FelicityVMS_PointsforAnalysi_lyr = "C:\\Path\\FelicityVMS_PointsforAnalysi.lyr"

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(VMS_InsideSDG, "INTERSECT", Public_Seed_Grounds_shp, "1 Miles", "NEW_SELECTION")

# Process: Copy Features
arcpy.CopyFeatures_management(Lake_Felicity, FelicityVMS_PointsforAnalysis100_shp, "", "0", "0", "0")

# Process: Integrate
arcpy.Integrate_management("C:\\Path\\FelicityVMS_PointsforAnalysis100.shp #", "100 Feet")

# Process: Collect Events
arcpy.CollectEvents_stats(FelicityVMS_PointsforAnalysis100_shp, FelicityVMS_PointsforAnalysis100_Collect_shp)

# Process: Hot Spot Analysis (Getis-Ord Gi*)
arcpy.HotSpots_stats(FelicityVMS_PointsforAnalysis100_Collect_shp, "ICOUNT", FelicityVMS_PointsforAnalysi_shp, "INVERSE_DISTANCE", "EUCLIDEAN_DISTANCE", "NONE", "", "", "")

# Process: ZScore Rendering
arcpy.ZRenderer_stats(FelicityVMS_PointsforAnalysi_shp, "GiZScore", FelicityVMS_PointsforAnalysi_lyr)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Add these lines before your select by location, change your select by location to the new feature layer.
Public_Seed_Grounds_lyr = "Public_Seed_Grounds_lyr" arcpy.MakeFeatureLayer_management(Public_Seed_Grounds_shp, Public_Seed_Grounds_lyr, "\"SEEDGRND\" = 'Lake Felicity'")  arcpy.SelectLayerByLocation_management(VMS_InsideSDG, "INTERSECT", Public_Seed_Grounds_lyr, "1 Miles", "NEW_SELECTION")

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Add these lines before your select by location, change your select by location to the new feature layer.
Public_Seed_Grounds_lyr = "Public_Seed_Grounds_lyr" arcpy.MakeFeatureLayer_management(Public_Seed_Grounds_shp, Public_Seed_Grounds_lyr, "\"SEEDGRND\" = 'Lake Felicity'")  arcpy.SelectLayerByLocation_management(VMS_InsideSDG, "INTERSECT", Public_Seed_Grounds_lyr, "1 Miles", "NEW_SELECTION")
0 Kudos
DeleteDelete
New Contributor
I really appreciate the help!  This worked perfectly.  However, I am having trouble applying this to the model I made.  It seems like I am missing a variable (The layer/feature to query the points). I have attached the model along with the new string, If you have the time...

Thanks again!

[ATTACH=CONFIG]26151[/ATTACH]


import arcpy


# Local variables:
SQL_Expression = "SELECT * FROM Public_Seed_Grounds WHERE \"SEEDGRND\" = 'Lake Felicity'"
Public_Seed_Grounds_shp = "C:\\PATH\\Public_Seed_Grounds.shp"
VMS_InsideSDG = "VMS_InsideSDG"
Lake_Felicity = "VMS_InsideSDG"
FelicityVMS_PointsforAnalysis100_shp = "C:\\PATH\\FelicityVMS_PointsforAnalysis100.shp"
FelicityVMS_PointsforAnalysis100_Collect_shp = "C:\\PATH\\FelicityVMS_PointsforAnalysis100_Collect.shp"
FelicityVMS_PointsforAnalysi_shp = "C:\\PATH\\FelicityVMS_PointsforAnalysi.shp"
FelicityVMS_PointsforAnalysi_lyr = "C:\\PATH\\FelicityVMS_PointsforAnalysi.lyr"
Public_Seed_Grounds_lyr = "Public_Seed_Grounds_lyr"
arcpy.MakeFeatureLayer_management(Public_Seed_Grounds_shp, Public_Seed_Grounds_lyr, "\"SEEDGRND\" = 'Lake Felicity'")

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(VMS_InsideSDG, "INTERSECT", Public_Seed_Grounds_lyr, "1 Miles", "NEW_SELECTION")

# Process: Copy Features
arcpy.CopyFeatures_management(Lake_Felicity, FelicityVMS_PointsforAnalysis100_shp, "", "0", "0", "0")

# Process: Integrate
arcpy.Integrate_management("C:\\PATH\\FelicityVMS_PointsforAnalysis100.shp #", "100 Feet")

# Process: Collect Events
arcpy.CollectEvents_stats(FelicityVMS_PointsforAnalysis100_shp, FelicityVMS_PointsforAnalysis100_Collect_shp)

# Process: Hot Spot Analysis (Getis-Ord Gi*)
arcpy.HotSpots_stats(FelicityVMS_PointsforAnalysis100_Collect_shp, "ICOUNT", FelicityVMS_PointsforAnalysi_shp, "INVERSE_DISTANCE", "EUCLIDEAN_DISTANCE", "NONE", "", "", "")

# Process: ZScore Rendering
arcpy.ZRenderer_stats(FelicityVMS_PointsforAnalysi_shp, "GiZScore", FelicityVMS_PointsforAnalysi_lyr)




Add these lines before your select by location, change your select by location to the new feature layer.
Public_Seed_Grounds_lyr = "Public_Seed_Grounds_lyr"
arcpy.MakeFeatureLayer_management(Public_Seed_Grounds_shp, Public_Seed_Grounds_lyr, "\"SEEDGRND\" = 'Lake Felicity'")

arcpy.SelectLayerByLocation_management(VMS_InsideSDG, "INTERSECT", Public_Seed_Grounds_lyr, "1 Miles", "NEW_SELECTION")
0 Kudos
DeleteDelete
New Contributor
Solved the issue.  Altered my model and now the python script works accordingly.  Posting for anyone whomay find this script for hot spot analysis helpful.

import arcpy


# Local variables:
Public_Seed_Grounds = "C:\\PATH\\Public_Seed_Grounds.shp"
Raw_Data = "C:\\PATH\\GIS\\Analysis\\VMS\\RawData.shp"
VMS_Events_Hackberry__1_mile_Layer_ = "RawData_Layer"
VMS_Events_Hackberry__1_mile_Shape_ = "C:\\PATH\\GIS\\Analysis\\VMS\\Model\\Test\\VMS_Data_HackberryBay.shp"
VMS_Events_Hackberry__150__Aggregation_ = "C:\\PATH\\GIS\\Analysis\\VMS\\Model\\Test\\Aggregate\\VMS_Data_Hackberry150_Aggregate.shp"
Hackberry_HotSpots__150__ = "C:\\PATH\\GIS\\Analysis\\VMS\\HotSpotAnalysis\\Hackberry_Hotspot150.shp"
Hackberry_ZScore__150__ = "C:\\PATH\\GIS\\Analysis\\VMS\\HotSpotAnalysis\\Hackberry_Hotspot150.lyr"
Seed_Ground_Selection = "Hackberry_Bay_Selection"
Raw_Data_Layer = "RawData_Layer"

# Process: Make Feature Layer (2)
arcpy.MakeFeatureLayer_management(Raw_Data, Raw_Data_Layer, "", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;ID ID VISIBLE NONE;ASSET_ID ASSET_ID VISIBLE NONE;SENT_DATE_ SENT_DATE_ VISIBLE NONE;LATITUDE LATITUDE VISIBLE NONE;LONGITUDE LONGITUDE VISIBLE NONE;ALTITUDE ALTITUDE VISIBLE NONE;SOG SOG VISIBLE NONE;COG COG VISIBLE NONE;POSITION_C POSITION_C VISIBLE NONE;AREA_IDS AREA_IDS VISIBLE NONE;AREA AREA VISIBLE NONE;ADDITIONAL ADDITIONAL VISIBLE NONE;SHIP_NAME SHIP_NAME VISIBLE NONE;LICENSE LICENSE VISIBLE NONE;REGISTRATI REGISTRATI VISIBLE NONE;INTEGER_ID INTEGER_ID VISIBLE NONE")

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(Public_Seed_Grounds, Seed_Ground_Selection, "\"SEEDGRND\" = 'Hackberry Bay'", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;SEEDGRND SEEDGRND VISIBLE NONE;SHAPE_Leng SHAPE_Leng VISIBLE NONE;SHAPE_Area SHAPE_Area VISIBLE NONE")

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Raw_Data_Layer, "INTERSECT", Seed_Ground_Selection, "1 Miles", "NEW_SELECTION")

# Process: Copy Features
arcpy.CopyFeatures_management(VMS_Events_Hackberry__1_mile_Layer_, VMS_Events_Hackberry__1_mile_Shape_, "", "0", "0", "0")

# Process: Integrate
arcpy.Integrate_management("C:\\PATH\\GIS\\Analysis\\VMS\\Model\\Test\\VMS_Data_HackberryBay.shp #", "150 Feet")

# Process: Collect Events
arcpy.CollectEvents_stats(VMS_Events_Hackberry__1_mile_Shape_, VMS_Events_Hackberry__150__Aggregation_)

# Process: Hot Spot Analysis (Getis-Ord Gi*)
arcpy.HotSpots_stats(VMS_Events_Hackberry__150__Aggregation_, "ICOUNT", Hackberry_HotSpots__150__, "INVERSE_DISTANCE", "EUCLIDEAN_DISTANCE", "NONE", "", "", "")

# Process: ZScore Rendering
arcpy.ZRenderer_stats(Hackberry_HotSpots__150__, "GiZScore", Hackberry_ZScore__150__)
0 Kudos