Skip to content

Instantly share code, notes, and snippets.

@knu2xs
knu2xs / preprocessing.py
Created July 11, 2022 18:27
Integration of Python API in Sci-Kit Transformers
View preprocessing.py
from functools import lru_cache
from typing import Union, List, Optional
from arcgis.geoenrichment import Country
from arcgis.geometry import Polygon
import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator, TransformerMixin
__all__ = ['EnrichBase', 'EnrichPolygon', 'EnrichStandardGeography', 'KeepOnlyEnrichColumns', 'ArrayToDataFrame']
@knu2xs
knu2xs / hashing.py
Last active November 29, 2022 16:46
Add a MD5 hash column to a Pandas data frame for change analysis.
View hashing.py
from hashlib import md5
import pandas as pd
from typing import Optional, Iterable
def get_md5_from_series(input_iterable: Iterable) -> str:
"""
Create a MD5 hash from an Iterable, typically a row from a Pandas ``DataFrame``, but can be any
Iterable object instance such as a list, tuple or Pandas ``Series``.
Args:
@knu2xs
knu2xs / get_logger.py
Created September 29, 2021 18:26
concisely get an easy to use logger
View get_logger.py
import logging
from pathlib import Path
from typing import Union
def get_logger(log_path: Union[str, Path] = 'logfile.log', log_name: str = 'logger', log_level: int = logging.ERROR):
"""
Standardized way to create a console and file logger in one.
Args:
View get_sys_info.py
import psutil
import platform
from datetime import datetime
def get_size(bytes, suffix="B"):
"""
Scale bytes to its proper format
e.g:
1253656 => '1.20MB'
1253656678 => '1.17GB'
@knu2xs
knu2xs / polsby_popper.py
Last active September 29, 2021 03:48
Calculate Polsby-Popper as measure of compactness
View polsby_popper.py
import math
from arcgis.geometry import Polygon
def get_simplified_polygon(geometry: Polygon, simplification_factor: float = 0.1) -> Polygon:
# ensure working with a polygon
assert isinstance(geometry, Polygon)
@knu2xs
knu2xs / environment.yml
Created September 28, 2021 21:58
Example conda environment file with PyDOBC
View environment.yml
name: arcgis
channels:
- esri
- conda-forge
dependencies:
- arcgis
- arcpy
- jupyterlab
View test_pro_version.py
def pro_at_least_version(version: str) -> bool:
"""
Test the current ArcGIS Pro version to be equal or greater than.
Args:
version: Version number eg ('2', '2.1', '2.8.1')
Returns:
Boolean indicating if version is at least or greater than specified version.
"""
# late import since may or may not already be loaded
View od-nax.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@knu2xs
knu2xs / enrichment-introspection.ipynb
Created May 28, 2021 15:12
Enrichment introspection examples
View enrichment-introspection.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@knu2xs
knu2xs / arcade_commute.arcade
Last active May 17, 2021 22:15
Arcade expression to calculate average drive time.
View arcade_commute.arcade
(
$feature.commute_ACSTWORKU5 * 2.5
+ $feature.commute_ACSTWORK5 * 7.0
+ $feature.commute_ACSTWORK10 * 12.0
+ $feature.commute_ACSTWORK15 * 17.0
+ $feature.commute_ACSTWORK20 * 22.0
+ $feature.commute_ACSTWORK25 * 27.0
+ $feature.commute_ACSTWORK30 * 32.0
+ $feature.commute_ACSTWORK35 * 37.0
+ $feature.commute_ACSTWORK40 * 42.0