GEOG 376 Final Review PDF

Title GEOG 376 Final Review
Course Introduction to Computer Programming for GIS
Institution University of Maryland
Pages 5
File Size 80.4 KB
File Type PDF
Total Downloads 51
Total Views 130

Summary

The final exam is non-cumulative, so this study guide should cover everything past the midterm, including how Python can interact with GIS software and libraries....


Description

GEOG 376 Final Review I.

Inputs and Outputs A. Text Files  They are often used as an interchange format for exchanging data between systems. They are easy to work with in python (You open the file first, then use Python’s functions to read/write from the file, and then close it) 1. Working with files in Python  Format: open(name[, mode[ , buffering]])  File.write() is used to write content to a file. Unlike the read() method, the write function requires a parameter

Command R (read)

Definition Placement It reads through the file that is Within the open function opened w Creates a new file for writing, Within the open function but if there is already an existing file, it will be overwritten a (append) Content is added at the end of Within the open function the file using the write method [file.append] File.read Read all the content from a Below the open function file into a single variable as one block of text Below the open function File.readlines Read each line of the file separately into a list (great when combined with a loop File.readline Read each line of the file Below the open function separately (often in a loop)  A + sign might be added for the r, w, or a commands when opening a file, which would do a little more  If you want to add content to the beginning or the middle of the file the seek method is used in place of append B. Reading from the Web  Programming today involves working with content from the web. The ‘urllib’ module allows us to access content from the web and bring in on to our own computer (or server) and manipulate it, combine it with other data, etc.  When content from the web is returned using urllib, it does not look like the actual content of the website when using Chrome or Firefox. It is written in HTML (Hypertext Markup Language), the language used to create websites  Some web platforms offer well structured content. For example Weather Underground (wunderground.com) offers an Application Programming Interface (API) that allows us to access weather data in comma delimited format. II. Classes and Objects A. Python as an Object-Oriented Programming Language

Object-oriented programming (OOP) focuses on creating reusable patterns of code. Procedural Programming focuses on explicit sequenced instuctions. B. Classes  A class is a blueprint for an object created by a programmer, which defines a set of attributes that will characterize any object that is instantiated from this class.  Class variables are defined within the class construction and are shared by all instances since they are owned by the class itself. They are placed right below the class header C. Objects  Objects are instances of classes and have property over instance variables  The instance variables are different for each object and are defined within methods  Self-parameter: The self parameter ensures that the methods have a way of referring to object attributes. When the methods are called, however, nothing is passed inside the parentheses, the object is being automatically passed with the dot operator.  Constructor: this method is used to initialize data automatically when a new object is created (known as the __init__ method). When used as a function, we do not need to explicitly call it, only pass the arguments in the parentheses following the class name when we create a new instance of the class. Any parameters added outside are passed on to the ___init__ method. D. Inheritance  Definition: when a class uses code constructed within another class. It is a way that object-oriented programming achieves recyclable code  The parent class creates a pattern that the child classes are based on, which allows us to create them via inheritance without writing the same code repeatedly  Child classes/subclasses inherit from the parent class, mainly the variables and the methods so they can make use of them. We can choose to add more methods, override existing parent methods, or simply accept the default parent methods with the pass keyword III. GDAL and OSGeo Modules A. Open Source Software  Open Source Software denotes any software for which the original source code is made freely available and may be redistributed and modified to meet your wn specifications in your own programs, unlike Proprietary Software, which is nonfree computer software for which the software’s publisher or another person retains intellectual property rights—usually copyright of the source code.  Most open source GIS tools (QGIS, gvSIG, GRASS) are built using Python, and can do just about anything that can be done in ArcGIS along with extra features that are usually not available. B. GDAL and OGR  GDAL (Geospatial Data Abstraction Library) is a translator library for raster and vector geospatial data formats that is released under an Open Source license by the Open Source Geospatial Foundation.  As a library, it presents a single raster abstract data model and single vector abstract data model to the calling application for all supported formats. (GDAL takes care of the raster data while OGR [OpenGIS Simple Features Reference Implementation] designs the vector segments for simple features) 

GDAL is not a python library, but provides a python interface module to using all the functionality. It also comes with a variety of useful command line utilities for data translation and processing. a. Creating a Shapefile 1. Import the GDAL module (ogr and osr) 2. Create a Spatial Reference for the shapefile 3. Create an empty Shapefile 4. Create a Layer for the shapefile and assign it to the Spatial Reference created earlier. 5. Create a set of Point Geometries as a Ring 6. Add the Ring in order to create a Polygon Geometry 7. Create a a Feature and add the Polygon Geometry to it (as well as any attributes). 8. Put the Feature in the Layer b. Reading a GeoTiff 1. Import GDAL Module 2. Open the GeoTiff Dataset 3. Print out the metadata for the dataset 4. Get the count of raster bands 5. Loop through all bands 6. Get the statistics for the current band 7. Print out those statistics 8. Close the GeoTiff dataset IV. Introduction to ArcPy  ArcPy is a suite of modules that allow us to do most of the things we can do in ArcGIS, but using the python programming language  ArcPy has access to geoprocessing tools as well as additional functions, classes, and modules that allow you to create simple or complex workflows. However, it is restricted by a license hierarchy  The Geoprocessing tools from ArcMap are available functions in arcpy that are accessed like any other Python function, and a distinction is always made between tool functions and non-tool ones. The tools return a Result object and produce messages through functions A. Environment Variables and Functions  ArcPy has environment variables that are set by default and are capable of affecting a geoprocessing tool’s results. These settings are available from the env class (arcpy.env…)  Functions in ArcPy can be used to list certain datasets, retrieve a dataset's properties, check for existence of data, validate a table name before adding it to a geodatabase, or perform many other useful scripting tasks. For example, the Describe function returns a Describe object, with multiple properties, such as data type, fields, indexes, and many others. B. Classes and Modules in ArcPy  In ArcPy, there are classes often used as shortcuts to complete geoprocessing tool parameters that would otherwise have a more complicated string equivalent. 

Classes used in ArcPy: (SpatialReference, Extent, ValueTable, and Point); their properties and methods (they are called constructors which can initialize a new instance of a class) can be used once instantiated  ArcPy Modules: arcpy.da (data access module), arcpy.mapping, arpy.sa (Spatial Analysis), arcpy.na (Network Analyst) V. Queries and Selections  Feature class is a table but with a geometry column, which allows us to view a geospatial map of a feature class.  A Feature Layer is what we find in the table of contents in ArcView (a link to the dataset, the symbology, selection information, etc.). The Table View is similar, but it does not have access to geometry A. Selecting a Layer using ArcPy  Selections using ArcPy only work with Layers, so you have to use the Make Feature Layer tool first [MakeFeatureLayer_management(in_features, out_layer, where_clause, )  in_features: the input feature class on which to make the new layer  out_layer: the name of the feature layer created  Select Layer by Location: selects features based on their location relative to features in another layer.  Select_Analysis: Extracts features from an input feature class or input feature layer, typically using a select or Structured Query Language (SQL) expression and stores them in an output feature class. [Select_Analysis (in_features, out_feature_class, (where_clause)) 1. Select Layer By Attribute  Primary use: to select features in a layer using an attribute query.  The Where clause in Select Layer By Attribute uses SQL syntax for query building and the expression is read as a contiguous string (shows up in green text in IDLE script). Wildcard symbols, when you want to conduct a partial string search, can change depending on the data source (File geodatabase: Percent symbol for multiple characters and underscore for one; Personal Geodatabase: Asterisk for many, Question mark for one) B. Queries  A query selects a subset of features/records which meet a specified criteria (either spatially or through attributive information). The features/records remain within the original Layer or Table  To continue working only with the subset you are interested in, copy this subset to a stand-alone feature / table [use arcpy.CopyFeatures_management(in_layer, outFeatures) and arcpy.CopyRows_management(intable, outTable)]  The syntax of a field name in ArcPy changes depending on the data source (The field name is surrounded by double quotes if taken from a shapefile/ file geodatabase or placed between square brackets if a personal geodatabase was used as a data source 1. Adding and Calculating fields arcpy.AddField_management (in_table, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain})  in_table - either feature class (e.g. shapefile or layer) 

field_name – string for the name of the column (avoid spaces, keep short) field_type – Text, Float, Double, Short, Long, Date, Blob, Raster, or Guid CalculateField_management: Calculates the values of a field for a feature class, feature layer, or raster [CalculateField_management(in_table, field, expression) C. Other ArcPy Features  arcpy.env.overwriteOutput = True; When importing ArcPy, use this command so you can overwrite existing settings and files if necessary  arcpy.Exists(dataset) – returns boolean True/False  arcpy.Delete_management(dataset): Data locks will prevent you from being able to execute this function   ...


Similar Free PDFs