Exploring & Manipulating Data in ArcPy
Population Data in New Mexico
Today we are going to write a script which will copy all of the data you see above (shapefiles) and put these shapefiles into a geo database that we created.
The city point layer we will use in the new GDB is now feature classes, and we are going to iterate over them using a search cursor.
The search cursor will use delimiters to filter out certain cities that we don't want. Our goal is to select cities that are only County Seat cities. Once we iterate over (search through) these cities, we want to list out these cities in a dictionary with the city name and population.
Let's Get Started!
- Creating a New GDB for our Python Script!
- In order to do this we need to import all of the modules to run our script. We also need to set up our environment, and the folder paths in which we are mapping to get our data. I am referencing the shapefiles above.
- We then create a new GDB using the Create File GDB management tool in ArcPY setting up the name of our new gdb and where we want to save it
- Then we can use the List Feature Classes tool and set it to a variable (list of feature classes in our project)
- We are going to use this variable and run a for loop, in which it will reference all of the fcs in our fc list.
- The for loop will then call arcpy.da.describe so we can use the base name's of our feature classes as the saved file name's when copying our data. Then we can use the Copy Feature Management too to now copy all of these fc's in our fc list to our newly created geo database!
- In order to do this we need to import all of the modules to run our script. We also need to set up our environment, and the folder paths in which we are mapping to get our data. I am referencing the shapefiles above.
- We then create a new GDB using the Create File GDB management tool in ArcPY setting up the name of our new gdb and where we want to save it
- Then we can use the List Feature Classes tool and set it to a variable (list of feature classes in our project)
- We are going to use this variable and run a for loop, in which it will reference all of the fcs in our fc list.
- The for loop will then call arcpy.da.describe so we can use the base name's of our feature classes as the saved file name's when copying our data. Then we can use the Copy Feature Management too to now copy all of these fc's in our fc list to our newly created geo database!
The files in my New GDB |
As you can see, we successfully copied all of the shapefiles in our project that we started with and saved them in a new geo database with the same names that they previously had. However, now all of our data is saved as feature classes instead of shapefiles.
Below You can see the data on the map, with pour cities being red dots, coutnies being different colored polygons, and so on...
New Mexico |
- Search cursors, Why are they useful?
- Now we are going to use a search cursor to iterate through one feature class in our project . this feature class is the cities layer which was displayed with red dots on our map. The goal here is to displays all of the cities in New Mexico that contain the County Seats attribute
- In order to do this, we are going to have to create a search cursor, then use a where clause (SQL Clause to pick this type of field, and eliminate all other cities that aren't county Seats) and establish the fields in our search cursor to iterate through.
- As a side note, there are different types of search cursors that we will jot use in this script. You can insert search cursors, use them for updates, or even delete data. It just depend on what you are trying to do...
- Now we are going to use a search cursor to iterate through one feature class in our project . this feature class is the cities layer which was displayed with red dots on our map. The goal here is to displays all of the cities in New Mexico that contain the County Seats attribute
- In order to do this, we are going to have to create a search cursor, then use a where clause (SQL Clause to pick this type of field, and eliminate all other cities that aren't county Seats) and establish the fields in our search cursor to iterate through.
- As a side note, there are different types of search cursors that we will jot use in this script. You can insert search cursors, use them for updates, or even delete data. It just depend on what you are trying to do...
In order to run our search cursor we need to do the following...
- Set our fc variable to a new file path that points to our cities feature class in our new GDB
- set fields to a variable containing the fileds that we want to use / filter out i our attribute table of cities
- Name
- Feature
- Pop_2000
- Now we can use the ArcPy. Add Field delimiters passing in the arguments of fc, fields, and the where clause as a cursor
- We need to create an empty dictionary called county_seats to save our output from our search cursor
- The we can do our For Loop to search for the rows in our cursor and index row 0 and row 2 for print out the city names and their population from our cities layer
- We need to add some print statements a swell to log what data we are iterating through in our For Loop
Look at the out below~
The results are the final output of my dictionary. The dictionary only lists cities that are County Seats, and the dictionary has the name of the city and the population amount.
The map below references these cities shown in the dictionary.
{'Aztec': 6378, 'Raton': 7282, 'Tierra Amarilla': -99999, 'Taos': 4700, 'Clayton': 2524, 'Mora': -99999, 'Los Alamos': -99999, 'Gallup': 20209, 'Mosquero': 120, 'Las Vegas': 14565, 'Bernalillo': 6611, 'Grants': 8806, 'Albuquerque': 448607, 'Tucumcari': 5989, 'Santa Rosa': 2744, 'Los Lunas': 10034, 'Estancia': 1584, 'Fort Sumner': 1249, 'Clovis': 32667, 'Socorro': 8877, 'Portales': 11131, 'Reserve': 387, 'Carrizozo': 1036, 'Roswell': 45293, 'Truth or Consequences': 7289, 'Silver City': 10545, 'Alamogordo': 35582, 'Lovington': 9471, 'Lordsburg': 3379, 'Deming': 14116, 'Las Cruces': 74267, 'Carlsbad': 25625}
See the map below to view the cities using our search cursor that are only county seats! The output above in green is the final dictionary using our search cursor.
This Python script in ArcPy shows how to create a new GDB, copy shapefiles over into feature classes, how to create and add search cursors, add delimiters, add where SQL clauses, and use for loops to iterate through a lot of data to print out specific attributes of data layers, their names, and certain outputs.
Check Out This Video On Search Cursors In ArcPy Using arc GIS Pro!
Comments
Post a Comment