In this video, Jennifer Harrison explains the concept of object-oriented programming and how Python can be used to access GIS objects. Learn about objects, methods, and properties.
- [Instructor] Python is an object-oriented scripting language. Object-oriented programming makes it easy to access the building blocks of a GIS because everything in the world is defined by certain classes, or types of objects, such as geodatabases and map documents. Each type of object has a set of characteristics, or properties. The properties can be read with the print statement, and some of the properties can be changed with a single assignment statement.
So, just the way you can say X equal three, you can also say map document.title equal map of California. Each type of object also has methods. Methods are things that the object knows how to do or things that you can do to the object. To work with Python and ArcPy objects, you don't have to know anything about the binary code behind the object. You just have to know what kind of object it is and what properties and methods that class of object supports.
Let's think of something in the real world that we can describe as an object. Let's say I tell you there's a class of things called TVs. All TVs have some common properties. They have a screen size, for example. They also have a similar set of methods, they can all be turned on. Now think of a single TV. A single TV is an object of the class TVs. It has properties, how big is the screen? Who manufactured it? What kind of connections does it have in the back? You can read the screen size or you can change the connection in the back from cable to internet, for example.
The TV also has methods. Methods are actions that make something happen, like turn on the TV, go to a certain channel, or turn up the volume. Methods may take parameters and may return a value. The turn on() method will return a false value if the TV is broken and it won't turn on. You don't need to know how to wire the electricity, you just need to find the on button.
The on button is like the TV's turn on() method. A turn up the volume() method might take a parameter that indicates how much you wanna turn it up and might return a value that shows the new volume level. One class of objects that ArcPy understands is the map document class. When you access a single map document, you're accessing a map document object. All map document objects have similar properties.
For example, each map document object has a relativePaths property. To find out whether the map was saved with relativePaths, print the relativePaths property. To change the map so that it is saved with relativePaths, update the relativePaths property by assigning it a new value. Each class of object also has methods. For example, a map document object has a Save a Copy() method.
The trick with learning to write Python scripts for GIS is learning the object classes and getting familiar with the properties and methods supported by each object class.
- Writing and running Python scripts in IDLE
- Making scripts flexible with variables
- Commenting scripts
- Manipulating string data
- Object-oriented programming in Python
- Creating loops
- Reading and writing text files
- Capturing input at runtime
- Documenting your scripts