Skip to content

Instantly share code, notes, and snippets.

@WindfallLabs
Created January 28, 2021 04:17
Show Gist options
  • Save WindfallLabs/23106e8bc11ad9b07dfeca731417f78c to your computer and use it in GitHub Desktop.
Save WindfallLabs/23106e8bc11ad9b07dfeca731417f78c to your computer and use it in GitHub Desktop.
# Strategy Pattern for GIS Data Processing
# As derived from:
# https://refactoring.guru/design-patterns/strategy/python/example#lang-features
from abc import ABC, abstractmethod
class Strategy(ABC):
# Using ABC (Abstract-Base-Class) and @abstractmethod forces
# the user to redefine the "main" method
@abstractmethod
def main(self):
pass
class KMLProcessor(Strategy):
def main(self, data):
# KML Processing code goes here
return # Return the result
class GPXProcessor(Strategy):
def main(self, data):
# GPX Processing code goes here
return # Return the result
class GeneralizedProcessor(object):
def __init__(self):
# Set the default strategy to None
self._strategy = None
@property
def strategy(self) -> Strategy:
# This is required for the following
return self._strategy
@strategy.setter
def strategy(self, strategy: Strategy) -> None:
# This enforces that the type is a subclass of Strategy
self._strategy = strategy
def process_data(self, geo_data):
"""General processing method."""
# This will change the "strategy" based on type, so it can handle .kml and/or .gpx files
if geo_data.endswith(".kml"):
self._strategy = KMLProcessor()
elif geo_data.endswith(".gpx"):
self._strategy = GPXProcessor()
result = self._strategy.main(geo_data)
return result
if __name__ == "__main__":
# Initialize the processor (for everything)
processor = GeneralizedProcessor()
processor.process_data(r"C:\pat\to\myData\data.kml")
processor.process_data(r"C:\pat\to\myData\data.gpx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment