#!/bin/sh
## Getting PLSS section data from the BLM
## Author: Dan Jacobson https://www.jidanni.org/
## Copyright: https://www.gnu.org/licenses/gpl.html
## Created: 2023-11-17T04:13:00+0000
## Last-Updated: 2024-06-22T05:29:09+0000
##     Update #: 61
##

if test $# -ne 0
then
    echo 1>&2 $0: no arguments allowed. Reads STDIN only.
    exit 42
fi

#Input: WA330160N0260E0SN070 %7C WA330160N02...
#or:    WA330160N0260E0      %7C WA330160N02... (i.e., townships too).

# Here I could look up PLSS IDs in order to know what to download!
# Townships:
# https://hub.arcgis.com/datasets/FDEP::bureau-of-land-management-blm-public-land-survey-system-township-map-service/explore?location=45.909622%2C-97.573900%2C10.92
# Sections:
# https://hub.arcgis.com/datasets/FDEP::bureau-of-land-management-blm-public-land-survey-system-intersected-map-service/explore?location=42.198562%2C-87.953041%2C16.85
# https://hub.arcgis.com/datasets/FDEP::bureau-of-land-management-blm-public-land-survey-system-section-map-service-/explore?location=41.982680%2C-87.691292%2C13.98
# https://hub.arcgis.com/datasets/BLM-EGIS::plss-township/explore?location=42.083752%2C-87.751784%2C14.66
# https://hub.arcgis.com/datasets/BLM-EGIS::plss-section/explore?location=14.811437%2C0.314800%2C2.40
# https://reports.blm.gov/docs/lr2000/CodeMeridian.pdf

# https://www.google.com/search?q=site:arcgis.com+plss+map+viewer

# http://www.earthsurvey.us/plss/plss.html
# https://gis.stackexchange.com/questions/182957/convert-plss-section-township-range-meridian-to-latitude-and-longitude-usin/291551#comment768695_291551
# https://gis.blm.gov/arcgis/rest/services/Cadastral/BLM_Natl_PLSS_CadNSDI/MapServer/exts/CadastralSpecialServices/GetLatLon
U=https://gis.blm.gov/arcgis/rest/services/Cadastral/BLM_Natl_PLSS_CadNSDI${N83+_NAD83}/MapServer/exts/CadastralSpecialServices/GetLatLon
# Well the NAD83 version differs from the default version, whatever that is,
# by 3.73 feet, at least in my central Dakotas check. Both say spatialReference wkid 4326 though...

curl \
    --url $U \
    --data-raw "trs=$(cat)&f=json" \
    --no-progress-meter \
    --compressed
#so pjson is pretty printed json, and html is html, and anything else is plain json.
#Output JSON...

# P.S., if the entire gis.blm.gov give HTTP 500, then just wait a few hours!
