Tuesday, June 24, 2025

Module 6: Geometries

 Module 6: Geometries

This weeks lab was about how to use geometries within Python and ArcGIS Pro. We wrote a script that creates a TXT file and write to it the coordinates and object IDs for vertices in a shapefile. This lab had us use two for loop statements. It was more difficult to understand how to make two for loop statements. Working with geometries is very time consuming but we learned about how to use geometry tokens. Geometry tokens provide shortcuts to specific geometry properties of individual features in a feature class. Another important factor we learned was when reading geometry types (polygons and polylines) requires extra code because an array of point objects is returned for each feature. As a result, an extra iteration is required to interact with the array first before you can get to the points that make up the array. This is where the for loop statements came in and made it easier to get all the information we may need. So that we did not have to do another for loop statement we did the getPart() method. The getPart() method returns a single point object for one single-part feature. If it is a polyline or polygon, it returns an array of point objects of the different features you are trying to get. In short, the getPart() method allows you to access and look through the different parts within a single geometry object. The hardest part I had was getting the information to print in the textfile and I remembered using the Try statements so that I could do the except and finally statements so that I could get the information to print. Being able to use those statements were important because it could show me where the issue was happening before things printed into the text file. Lastly, my other issue was figuring out the .write() statement so the information did actually write into the textfile. That is when I created the line I wanted to write and then put it the parentheses. The image below shows a portion of the information into the textfile.

This image below is the flowchart of how I created the script for the information I wanted for the rivers shapefile. Once again it was difficult to show the for loops and how they were being represented.

This module was probably the most difficult one because of the two for loop statements and getting the information to write into the textfile. Once I remembered the try statement I was able to make sure I got all the information correctly into the textfile.

Tuesday, June 17, 2025

Module 5: Explore and Manipulate Data

 Module 5: Explore and Manipulate Data

In this weeks module the focus was on how we can explore and manipulate data that are within ArcGIS Pro. The script creates a new file geodatabase, copy feature class data to the new file geodatabase, create a Search Cursor for the cities feature class, and create a new dictionary.

First we needed to create a new file geodatabase that goes into the results folder. It is important to note that before any of this can begin you need to make sure you import arcpy and import env. Also, to make sure you can create the geodatabase more than once you need to have the overwrite output to True. If you are getting an error message if trying it again it is because ArcGIS Pro is open and it needs to be closed before running it in Python IDLE. If you run it in the Notebook portion in ArcGIS Pro you do not seem to have that problem. The image below shows the outcome of the script that was written.

The next portion of the script was creating a feature class list and copying that into the new geodatabase that was previously created. When making sure the data gets copied you need to use the Describe() function. Also, to get all the data copied you need to make sure to the script as the following arcpy.CopyFeatures_management(fc, outputEnv + "/ejd31.gdb/" + fcdesc.basename). This piece of code makes sure everything gets copied and that the basename of the feature shows up when it gets copied. In the image below shows that each feature got copied and how long it took to get the information to copy into the new file geodatabase.

After that I needed to create a Search Cursor that only printed the Name, Feature, and Pop_2000 only for the county seat within the cities shapefile. I knew how to write the code for one row in the attribute table but was not sure for three. I made the fields code as fields = [“NAME”, “FEATURE”, “POP_2000”] and the sqlQuery = “FEATURE = ‘County Seat’”.  I used the arcpy.da.SearchCursor because I did not want to change the table but to just find the specific rows and print them. Also, you need to make sure to use a for loop statement to make sure only the specific rows get printed and not the other ones in the attribute table. The image below shows all the features that were only County Seat with the name and population of those features.
     

The last portion of the script was creating a new dictionary based off the information from the cities shapefile attribute table. The dictionary needs to focus on the name and population of the county seat features. Another issue I was having was trying to get the for loop statement to create a dictionary for an empty dictionary. I knew how to make the dictionary but was not sure how to make the statement work. I did try different versions and none were working for me. I think asked the discussions page and Serenity helped me with the script needed to have another line of code saying with arcpy.da.SearchCursor(fc, fields, sqlQuery) as cursor: The information in the parentheses is based off the previous script above. The difference is that I needed to add the specific row numbers for the keys and values that I wanted represented in the dictionary. Lastly, I chose the dictionary variable[keys] = variables which was county_seats[name] = population. The image below shows the new dictionary with just the information of the Name and Population of the county seats in the cities shapefile

An important note is that to get the copying features to print separately the code is print(f”Copying: {fc} \n”). This was able to help me get them to print one at a time instead of all of them at once over and over again. Knwoing how to get the information to print properly will help know what is being copied in the first place. The script creates a new file geodatabase, copy feature class data to the new file geodatabase, create a Search Cursor for the cities feature class, and create a new dictionary. The image is of the flowchart that I used for the script that was written. 



Monday, June 9, 2025

Module 4: Geoprocessing

 Module 4: Geoprocessing

In this weeks module the focus was on geoprocessing tools, creating our own ModelBuilder and toolboxes. The first portion of the assignment was using the ModelBuilder to clip sols within the basin shapefile, select a specific attribute within the soils shapefile, and erase the "Not prime farmland" within the soils shapefile. Within the ModelBuilder the you can drag in your data and geoprocessing tools. These tools are similar as ones that I have used within the maps. The ModelBuilder is important because you can create what you want done all at once instead of each step separately. In the image below it shows how I added the basin and soils shape file. Next added in the clip analysis tool where I said I wanted the soils only within the basin shapefile. Which creates the shapefile which I called soil_Clip. Then to erase the "Not prime farmland" portion I needed to add the Select analysis tool which I created a script. Lastly, this created Soils_Selected.shp.


This image below shows what the ModelBuilder produced which was all the areas that were not apart of the "Not prime farmland" shapefiles.


The next portion of this module was writing script that adds XY coordinates, creates a 1000 meter buffer, and dissolve the buffers into a single feature for the hospitals shapefile. When designing the script that adds the XY coordinates, buffers, and dissolve the hospital shapefile I needed to make sure there is arcpy and env workspace. The workspace is the location of where the data is coming from to add the information to the shapefile. Next, I made sure to overwrite the output features so the code could be done repeatedly. The first portion of the code was the XY coordinates on the shapefile by creating the shapefile as the in_features. To make sure the shapefile gets the XY coordinates the code needs to be “arcpy.management.AddXY(in_features)”. For the buffer portion I needed to write “arcpy.Buffer_analysis” Within this code I need to make sure to put in the buffer zone and where I want the shapefile to go which was the results tab in Module. Lastly, for the dissolving portion it is the same code as the buffer analysis, but I needed to make sure to add two open string portions and one that says ‘ALL’. This will make sure all the areas that overlap dissolve and create a whole new shapefile. If I did not want to create a new shapefile, I could name it the same shapefile as above. Another thing that needed to be added was the GetMessages() function so you can see the start time and if the script was successful in running. I put this in after the the other print option. The code was written as "print(arcpy.GetMessages())". Within ArcGIS Pro you do not necessarily need to add arcpy but I still added “import arcpy” in case the script runs in IDLE. Also, to make sure things are overwritten you need to make sure the box in clicked in the options tab under geoprocessing and write the code “env.overwriteOutput = True”. Lastly, to make a space within your script you can use “\n” within the print portion of the script. The image below is the result of the script that I wrote to add the XY coordinates, 1000 meter buffer, and dissolve the buffer for the hospitals shapefile.

This weeks module was very useful because it is able to learn how to use ModelBuilder to make shapefiles all at once instead of one at a time. Also, I learned how to do Batch processing that can help me do a lot of data all at once. Lastly, I was able to learn how to create a script that can add XY coordinates, add a buffer, and dissolve that buffer instead of doing it through the geoprocessing tools individually.

Monday, June 2, 2025

Module 3: Debugging and Error Handling

 Module 3: Debugging and Error Handling

In this weeks module the focus was on how to debug code and understand what the error messages are saying to the person writing or using scripts. There are different errors that can be seen when writing script. The first common error message is syntax error as known as the "grammar" of the code. This includes the spelling, punctuation, and indentation. On the other hand there are run-time errors that come up after the code gets run. Some examples are NameError, TypeError, and LicenseError. When doing this week we did three different scripts that we either fixed or did a try-except statment.

In the first script we had to look for two errors that were within the code. When trying to figure out the errors you can either click the "Run Module" or "Check Module" within IDLE. For me the easiest one was to save the script and clicking "Run Module". Then it comes out saying what the error is and what line it is located on. The first error was a NameError that within Line 5 the name 'fc' is not defined. When looking at the rest of the code it showed that the name 'fc' needed to be capitalized. The second error was another NameError in Line 8 where the name 'field' was not defined. Once again looking at the rest of the script I was able to notice I needed to add an 's' to the end to match other portions of the script. The picture below is the output of the script that was written.


The second script there were several errors or exceptions. Once again it was easy to save the script and click "Run Module" to find the first error. The first error was a NameError in Line 6 that indicated 'Filepath' is not defined. When looking in the rest of the code you can see it needed to be 'filepath' in the parenthesis. Next was an OSError which indicates that the file path in Line 6 is not connected to anything on the computer. I fixed the OSError to filepath ="S:/GISProgramming/Module3/Data/ Mod3_Data/TravisCountyAustinTX.aprx". The next error was a NameError in Line 8 saying 'arcp' is not defined and I noticed it was the dataset 'arcpy' that needed to fixed. Afterwards there was another OSError found in Line 7 and noticed one of the slashes were in the wrong way and that I needed to add more to the dataset file path. Then the next error was AttributeError that says 'spatialRefernce" does not exist. All I had to do was spell it correctly as "spatialReference." The next error was a TypeError that says 'type' object is not iterable in Line 13 and all I needed to do is add a 's' to the end of map. After fixing all the errors and exceptions it finally produced the outcome of the spatial reference and the four shapefiles within the ArcGIS Pro map which is seen in the picture below.


The third and final script that was written had errors but we needed to do a try-except statement. This statement allows a script to run successfully which helps with seeing the exceptions within the code and “trap” it so the code can continue run. When including the try-except statement you want to run the code to see what the error is that is stopping the code from running. First you want to put “try” statement before the code that has the error(s). Next you want to then put the “except” statement below the error and handles the error. Then you will run the code again to see if there are any other errors in the code. If there is another error, you can include in the current “except” statement or make another. Lastly, when you have all the errors it should be able to run the code and explain to someone why there were some errors as well as where they were located. I have included the output of both the parts and a flow chart of how to do Part A of the try-except statement.


In this module it was very helpful to learn about the different errors so that in the future my script runs without issues. Also, if I do run into issues I can fix it easily by being able to read what the errors and fixing them. Lastly, learning how to do the try-except statement lets the code run even if there are some errors that you might not necessarily know how to fix at the moment.

Blog Post #5: GIS Portfolio

 Blog Post #5: GIS Portfolio In the final weeks for the GIS Internship we were given the task of creating a GIS portfolio either on paper or...