Skip to content

Instantly share code, notes, and snippets.

@breinbaas
Created May 5, 2023 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breinbaas/1323211397e550ed001220bbfaa4d7c1 to your computer and use it in GitHub Desktop.
Save breinbaas/1323211397e550ed001220bbfaa4d7c1 to your computer and use it in GitHub Desktop.
"""
Script documentation
Get all BRO cpt's from the current ArcGis Map extent. Add the script to your toolbox.
And make sure to add three parameters;
Output Path [Folder]: where to write the cpts to
Start Date [Date]: start date CPT (NOT before 01-01-2015)
End Data [Date]: end date CPT
Will write the found xml files to the given output path
"""
import arcpy
from datetime import datetime
from pyproj import Transformer
from bro import Envelope, Point, get_cpt_characteristics_and_return_cpt_objects
from geotexxx.gefxml_reader import Cpt
from pathlib import Path
def script_tool(ext, pcscode, start_date, end_date, output_path):
if pcscode != 4326:
tf = Transformer.from_crs(pcscode, 4326)
lat1, lon1 = tf.transform(ext.XMin, ext.YMin)
lat2, lon2 = tf.transform(ext.XMax, ext.YMax)
else:
lat1, lon1 = ext.XMin, ext.YMin
lat2, lon2 = ext.XMax, ext.YMax
if start_date < datetime(2015, 1, 1):
start_date = datetime(2015, 1, 1)
lower_corner = Point(min(lat1, lat2), min(lon1, lon2))
upper_corner = Point(max(lat1, lat2), max(lon1, lon2))
area = Envelope(lower_corner, upper_corner)
try:
cpts_as_xml = get_cpt_characteristics_and_return_cpt_objects(
start_date.strftime("%Y-%m-%d"),
end_date.strftime("%Y-%m-%d"),
area,
as_dict=False,
)
for cpt_xml in cpts_as_xml:
cpt = Cpt()
cpt.load_xml(cpt_xml, fromFile=False)
name = cpt.testid
with open(Path(output_path) / f"{name}.xml", "w") as f:
f.write(cpt_xml.decode("utf-8"))
except Exception as e:
arcpy.AddMessage(f"Could not retrieve cpts, got error '{e}'")
return []
if __name__ == "__main__":
output_path = arcpy.GetParameterAsText(0)
start_date = arcpy.GetParameterAsText(1)
end_date = arcpy.GetParameterAsText(2)
start_date = datetime.strptime(start_date, "%m/%d/%Y")
end_date = datetime.strptime(end_date, "%m/%d/%Y")
# get the current coordinate system
aprx = arcpy.mp.ArcGISProject("current")
m = aprx.listMaps()
mp_projection = m[0].spatialReference
# get the extent of the view
mv = aprx.activeView
ext = mv.camera.getExtent()
# get the cpts
script_tool(ext, mp_projection.PCSCode, start_date, end_date, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment