Lab1b_2#

Variables and Data Types#

Overview#

This lecture introduces the fundamental concepts of variables, and data types in Python, focusing on their applications within geospatial programming. Understanding these basics is essential for working with geospatial data, as variables allow you to store and manipulate information, while data types define the kind of operations you can perform on this information. We will also explore the fundamental Python data structures: Tuples, Lists, Sets, and Dictionaries. These data structures are essential tools in geospatial programming, enabling you to efficiently store, manage, and manipulate various types of data. By mastering these structures, you will be able to handle complex geospatial datasets with ease, paving the way for more advanced analysis and processing tasks.

Learning Objectives#

By the end of this lecture, you should be able to:

  • Define and use variables in Python, adhering to best practices for naming and assignment.

  • Identify and utilize various Python data types, including integers, floats, strings, booleans, lists, and dictionaries.

  • Understand how data types are used in the context of geospatial data, such as coordinates and attribute information.

  • Understand the characteristics and use cases of Python tuples, lists, sets, and dictionaries.

  • Apply these data structures to store and manipulate geospatial data, such as coordinates, paths, and attribute information.

  • Perform common operations on these data structures, including indexing, slicing, adding/removing elements, and updating values.

  • Utilize dictionaries to manage geospatial feature attributes and understand the importance of key-value pairs in geospatial data management.

Variables in Python#

In Python, a variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to that object by the variable name.

Let’s start by creating a simple variable that represents the number of spatial points in a dataset.

num_points = 120

This variable num_points now holds the integer value 120, which we can use in our calculations or logic.

To view the value of the variable, we can use the print() function.

print(num_points)

Alternatively, we can simply type the variable name in a code cell and run the cell to display the value of the variable.

num_points

Naming Variables#

When naming variables, you should follow these rules:

  • Variable names must start with a letter or an underscore, such as _.

  • The remainder of the variable name can consist of letters, numbers, and underscores.

  • Variable names are case-sensitive, so num_points and Num_Points are different variables.

  • Variable names should be descriptive and meaningful, such as num_points instead of n.

  • Avoid using Python keywords and built-in functions as variable names, such as print, sum, list, dict, str, int, float, bool, set, tuple, range, type, object, None, True, False, and, or, not, if, else, elif, for, while, break, continue, pass, def, return, lambda, class, import, from, as, with, try, except, finally, raise, assert, del, in, is, global, nonlocal, yield, async, await.

Data Types#

Python supports various data types, which are essential to understand before working with geospatial data. The most common data types include:

a) Integers (int): These are whole numbers, e.g., 1, 120, -5

num_features = 500.  # Represents the number of features in a geospatial dataset
float

b) Floating-point numbers (float): These are numbers with a decimal point, e.g., 3.14, -0.001, 100.0. You can write multiple lines of code in a single code cell. The output will be displayed for the last line of code.

latitude = 35.6895  # Represents the latitude of a point on Earth's surface
longitude = 139.6917  # Represents the longitude of a point on Earth's surface

c) Strings (str): Strings are sequences of characters, e.g., “Hello”, “Geospatial Data”, “Lat/Long”

coordinate_system = "WGS 84"  # Represents a commonly used coordinate system

Strings can be enclosed in single quotes (') or double quotes ("). You can also use triple quotes (''' or """) for multiline strings.

d) Booleans (bool): Booleans represent one of two values: True or False

is_georeferenced = True  # Represents whether a dataset is georeferenced or not

Data Structures#

Tuples#

Tuples are immutable sequences, meaning that once a tuple is created, its elements cannot be changed. Tuples are useful for storing fixed collections of items.

For example, a tuple can be used to store the coordinates of a geographic point (latitude, longitude).

point = (
    35.6895,
    139.6917
)  # Tuple representing a geographic point (latitude, longitude)

You can access elements in a tuple using indexing:

The f in the print statement refers to an f-string in Python, which is a way to format strings. Introduced in Python 3.6, f-strings provide a concise and convenient way to embed expressions inside string literals, using curly braces {}.

latitude = point[0]
longitude = point[1]
print(f"Latitude: {latitude}, Longitude: {longitude}")
# try point [2], point [-1], point [-2] to see what will happen?
Latitude: 35.6895, Longitude: 139.6917
#it can be anything inside of tuple, nested tuple
cities = (("Batong Rouge", "Lafayette"),("Chicago", "New York"))
# how to access to Chicago from this Tuple
cities[1][0]
'Chicago'

Lists#

Lists are ordered collections of items, which can be of any data type.

Lists are ordered, mutable sequences, meaning you can change, add, or remove elements after the list has been created. Lists are very flexible and can store multiple types of data, making them useful for various geospatial tasks.

For example, you can store a list of coordinates representing a path or boundary.

path = [
    (35.6895, 139.6917),
    (34.0522, -118.2437),
    (51.5074, -0.1278),
]  # List of tuples representing a path

You can add a new point to the path:

path.append((48.8566, 2.3522))  # Adding Paris to the path
print("Updated path:", path)
Updated path: [(35.6895, 139.6917), (34.0522, -118.2437), (51.5074, -0.1278), (48.8566, 2.3522)]
len (path)
5
path.clear()
path
[]

Lists allow you to perform various operations such as slicing, which lets you access a subset of the list:

sub_path = path[:2]  # Slicing the first two points from the path
print("Sub-path:", sub_path)
Sub-path: [(35.6895, 139.6917), (34.0522, -118.2437)]

Dictionaries#

Dictionaries are collections of key-value pairs, where each key is unique. Dictionaries are extremely useful for storing data that is associated with specific identifiers, such as attribute data for geographic features.

For example, you can use a dictionary to store attributes of a geospatial feature, such as a city. You will encounter dictionaries all the time, if you deal with geospatial data, such as jason file.

city_attributes = {
    "name": "Tokyo",
    "population": 13929286,
    "coordinates": (35.6895, 139.6917),
}  # Dictionary storing attributes of a city

You can access the values associated with specific keys:

city_name = city_attributes["name"]
city_population = city_attributes["population"]
print(f"City: {city_name}, Population: {city_population}")
City: Tokyo, Population: 13929286
city_attributes["population"] = 140000000 #update the number of population
city_attributes
{'name': 'Tokyo', 'population': 140000000, 'coordinates': (35.6895, 139.6917)}
# Update coodinates
city_attributes["coordinates"] = (30, -90)
city_attributes
{'name': 'Tokyo', 'population': 140000000, 'coordinates': (30, -90)}

You can also add or update key-value pairs in a dictionary:

city_attributes["area_km2"] = 2191  # Adding the area of the city in square kilometers
print("Updated city attributes:", city_attributes)
Updated city attributes: {'name': 'Tokyo', 'population': 13929286, 'coordinates': (35.6895, 139.6917), 'area_km2': 2191}

Homework 2 due by 09/08/2024#

Create a dictionary to store attributes of a geographic feature (e.g., a river or mountain). Include keys for the name, length, and location of the feature. Then, add an additional attribute (e.g., the source of the river or the height of the mountain) and print the dictionary.

# Type your code here

Conclusion#

Understanding and utilizing Python’s data structures such as tuples, lists, sets, and dictionaries are fundamental skills in geospatial programming. These structures provide the flexibility and functionality required to manage and manipulate spatial data effectively.

Continue exploring these data structures by applying them to your geospatial projects and analyses.