top of page

Python re Library: A Guide to Text Processing for GIS

Writer: Anvita Shrivastava
Anvita Shrivastava
2 days ago
6 min read

Geospatial data processing involves more than maps, satellite images, and geographic coordinates. Geographic Information Systems (GIS) use textual data as well. This textual data includes place names, addresses, coordinates, metadata, spatial identifiers, and accurate attribute descriptions.


Extracting and cleaning text from significantly large data sets can take a lot of time when done manually. Python provides a powerful tool which is called re (regular expressions) and enables GIS specialists to search, extract, validate, and process textual data by means of patterns.


Python re library is very useful for processing geographic coordinates, cleaning spatial attribute tables, reconstructing place names, extracting relevant metadata from GIS structures, and automating the geospatial preparation process.


Python re Library
Python re Library

What Is the Python re Library?


Python re library is an inbuilt Python library for carrying out text processing work with the help of regular expressions. These expressions consist of a series of characters serving the purpose of search patterns employed in text identification and extraction, text replacement, and text validation.


Regular expressions give an advantage over regular string operations by enabling identification of complex text patterns according to certain predefined rules.


When it comes to the example from GIS datasets, they might have geographical coordinates in the following form:


Latitude: 40.7128

Longitude: -74.0060


With the help of Python's re library, a developer can automatically extracted these coordinates from a big number of texts.


Why Is the re Library Important for GIS?


GIS applications usually have unstructured or semi-structured text data.


Some examples are:

  • Geographic coordinates kept as strings.

  • Address and location data.

  • Spatial reference identifiers like EPSG codes.

  • Satellite image metadata.

  • GIS attribute table definitions.

  • GPS tracking logs.

  • Geographic feature names.

  • Mapping and surveying reports.

The Python re library helps convert these kinds of information into structured data ready for spatial analysis.


How to Import the Python re Library


The re module is included in Python's standard library, so no additional installation is required.

import re

After importing the module, developers can perform pattern matching, text extraction, substitution, and validation.


Important Functions in the Python re Library


The following functions are commonly used for text processing in GIS applications.

Function

Description

GIS Application

Finds the first matching pattern

Locate coordinates in metadata

re.findall()

Returns all matching occurrences

Extract multiple EPSG codes

re.match()

Matches a pattern at the beginning

Check coordinate prefixes

re.fullmatch()

Validates an entire string

Validate coordinate formatting

re.sub()

Replaces matching text

Clean GIS attributes

re.split()

Splits text using a pattern

Separate coordinate values

re.compile()

Compiles reusable patterns

Process large GIS datasets


  1. Extract Geographic Coordinates Using Python re


Geographic coordinates are frequently stored in text files, GPS logs, and geographic metadata.

Consider the following string:

import retext = "Location: 40.7128, -74.0060"pattern = r"(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)"match = re.search(pattern, text)if match:    latitude = float(match.group(1))    longitude = float(match.group(2))    print("Latitude:", latitude)    print("Longitude:", longitude)

Output:

Latitude: 40.7128
Longitude: -74.006

Technical Explanation


The regular expression contains several components:

  • -? allows an optional negative sign.

  • \d+ matches one or more digits.

  • (?:\.\d+)? matches an optional decimal portion.

  • ,\s* matches a comma followed by optional whitespace.

  • Parentheses capture the coordinate values.


This technique is useful for extracting latitude and longitude from GPS reports, GIS logs, and text-based spatial datasets.

However, regex only identifies the coordinate format. Additional validation is required to ensure latitude values fall between -90 and 90 and longitude values between -180 and 180.


  1. Extract EPSG Codes from GIS Metadata


Coordinate Reference Systems (CRS) define how geographic locations are represented.

Many GIS datasets use EPSG identifiers to specify their spatial reference systems.

For example:

Coordinate System: EPSG:4326
Projection: EPSG:32633

Python regex can extract these identifiers automatically.

import remetadata = """Coordinate System: EPSG:4326Projection: EPSG:32633"""pattern = r"EPSG:\s*(\d+)"epsg_codes = re.findall(pattern, metadata)print(epsg_codes)

Output:

['4326', '32633']

GIS Applications


Through this technique, it is possible to facilitate the automatic extraction of CRS identifiers present in metadata reports, projection explanations, and spatial data inventories.


To correctly interpret CRS and convert coordinates, use particular libraries such as pyproj. Regular expressions cannot solely determine whether a CRS identifier exists and whether it is suitable for a dataset.


  1. Clean GIS Attribute Tables Using re.sub()


Geospatial attribute tables often contain inconsistent names, special characters, and unnecessary whitespace.

For example:

New-York_City!!
Los Angeles###
San Francisco@@

Python's re.sub() function can standardize these values.


import re


locations = [

"New-York_City!!",

"Los Angeles###",

"San Francisco@@"

]


cleaned_locations = []


for location in locations:

cleaned = re.sub(r"[^A-Za-z\s]", " ", location)

cleaned = re.sub(r"\s+", " ", cleaned).strip()

cleaned_locations.append(cleaned)


print(cleaned_locations)


Output:

['New York City', 'Los Angeles', 'San Francisco']

  1. Process Geographic Data Using Pandas and Regex


Python's Pandas library can be combined with regular expressions to process large tabular datasets.

Consider a CSV file containing geographic identifiers.

Location,CRS
New York,EPSG:4326
London,EPSG:27700
Tokyo,EPSG:6677

The following example extracts EPSG codes from the CRS column.


import pandas as pd


df = pd.DataFrame({

"Location": ["New York", "London", "Tokyo"],

"CRS": ["EPSG:4326", "EPSG:27700", "EPSG:6677"]

})


df["EPSG_Code"] = df["CRS"].str.extract(

r"EPSG:(\d+)"

)


print(df)


Output:

   Location         CRS EPSG_Code
0  New York   EPSG:4326      4326
1    London  EPSG:27700     27700
2     Tokyo   EPSG:6677      6677

This approach allows GIS developers to process entire columns without manually iterating through individual records.


  1. Extract Coordinate Information from Satellite Metadata


Remote sensing datasets often include metadata describing acquisition dates, sensor information, geographic coordinates, and image properties.

For example:

Satellite: Landsat 8
Acquisition Date: 2026-05-15
Cloud Cover: 12.5%

Python regex can extract acquisition dates and cloud-cover percentages.


import re


metadata = """

Satellite: Landsat 8

Acquisition Date: 2026-05-15

Cloud Cover: 12.5%

"""


date_pattern = r"\d{4}-\d{2}-\d{2}"

cloud_pattern = r"Cloud Cover:\s*(\d+(?:\.\d+)?)%"


date = re.search(date_pattern, metadata)

cloud = re.search(cloud_pattern, metadata)


if date and cloud:

print("Acquisition Date:", date.group())

print("Cloud Cover:", cloud.group(1), "%")


Output:

Acquisition Date: 2026-05-15
Cloud Cover: 12.5 %

The technique is effective in developing metadata indexing pipelines, filtering satellite imagery databases, and processing remote sensing datasets.


When it comes to structured satellite metadata formats like STAC JSON, the specialized JSON parsers are favored over regex-based methods due to their ease of implementation.


  1. Use Python re with GeoPandas for Spatial Data Cleaning


GeoPandas is a Python library designed for working with geospatial vector datasets.

It extends Pandas by supporting geographic geometry and spatial operations.

Regular expressions can be applied to GeoPandas attribute columns.


Example: Standardize Geographic Feature Names


import geopandas as gpd


gdf = gpd.read_file("cities.geojson")


gdf["clean_name"] = (

gdf["city_name"]

.fillna("")

.str.replace(

r"[^A-Za-z\s]",

" ",

regex=True

)

.str.replace(

r"\s+",

" ",

regex=True

)

.str.strip()

)


gdf.to_file(

"cleaned_cities.geojson",

driver="GeoJSON"

)


Technical Workflow


  1. The geographic information is read from the GeoJSON file.

  2. The naming of the city is selected using its attribute.

  3. Regex is used to remove encoding characters.

  4. The spaces are normalized.

  5. A new variable with the cleaned data is created.

  6. The data is exported in its revised version.


Geospatial database creation can benefit from this workflow.


  1. Validate Latitude and Longitude Using Python re


GIS applications frequently receive coordinate information from external sources.

Incorrect coordinate formats can cause errors during data import or spatial processing.

A combination of regex and numerical validation provides a more reliable solution.


import re


def validate_coordinates(text):

pattern = (

r"^\s*(-?\d+(?:\.\d+)?)"

r"\s*,\s*"

r"(-?\d+(?:\.\d+)?)\s*$"

)


match = re.fullmatch(pattern, text)


if not match:

return False


latitude = float(match.group(1))

longitude = float(match.group(2))


return (

-90 <= latitude <= 90

and -180 <= longitude <= 180

)


print(validate_coordinates("40.7128, -74.0060"))

print(validate_coordinates("120.0000, -200.0000"))


Output:

True
False

This validation assumes the input follows the latitude-longitude order in decimal degrees. It does not support every valid geographic coordinate notation, such as degrees-minutes-seconds.


  1. Improve Regex Performance with re.compile()


When processing thousands or millions of GIS records, repeatedly using the same regular expression can introduce unnecessary overhead.

The re.compile() function creates a reusable regex pattern object.


import re


coordinate_pattern = re.compile(

r"(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)"

)


records = [

"40.7128, -74.0060",

"34.0522, -118.2437",

"51.5074, -0.1278"

]


for record in records:

match = coordinate_pattern.search(record)


if match:

print(match.groups())


Output:

('40.7128', '-74.0060')
('34.0522', '-118.2437')
('51.5074', '-0.1278')

Compiled expressions are very beneficial in case of using certain expressions multiple times. Python has the feature of caching recently compiled regular expressions; thus, compiling them does not guarantee a noticeable increase in performance for every type of workload.


The re library in Python is a very helpful tool in processing text and preparing data for use in Geographic Information Systems. With the help of regular expressions, developers can easily retrieve geographical coordinates, recognize CRS codes, clean up spatial information, and validate coordinate strings.


If used together with Python libraries like Pandas, GeoPandas, PyProj, or Rasterio, Python regex can help GIS experts automate their workflows and create consistent data, which reduces the amount of work done manually.


Given the increasing number of geospatial databases, it is essential for developers to learn to use Python regular expressions in GIS.


To learn more about the re Library and its geospatial capabilities, click here.


For more information or any questions regarding the LizardTech suite of products, please don't hesitate to contact us at:



USA (HQ): (720) 702–4849


(A GeoWGS84 Corp Company)



Comments


bottom of page