Skip to content

Commit

Permalink
Custom server w/ dropdown (#11)
Browse files Browse the repository at this point in the history
* Initial stages of drop-down

* made it so that it doesn't yell at you for using a custom server as well as adding a custom folder for your outputs

* change message to error for invalid url

* cleanup

* fixed bare exception, parameter messaging, and removed unique directory output

* ran black

---------

Co-authored-by: Ogi Moore <[email protected]>
  • Loading branch information
willforker and j9ac9k authored Mar 11, 2024
1 parent d269176 commit 872c5bf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 31 deletions.
91 changes: 61 additions & 30 deletions src/doppkit_toolbox/esri/toolboxes/doppkit_wrapper_toolbox.pyt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from doppkit.app import Application
from doppkit.grid import Grid
from doppkit.cli.sync import sync
import asyncio
import time

from typing import NamedTuple

Expand Down Expand Up @@ -46,7 +47,13 @@ class FetchExport:
parameterType="Required",
direction="Input",
)

# specify the default server
grid_server.filter.list = [
"https://grid.nga.mil/grid",
"https://grid.nga.smil.mil",
"https://grid.nga.ic.gov",
]
grid_server.value = "https://grid.nga.mil/grid"

grid_access_token = arcpy.Parameter(
Expand Down Expand Up @@ -77,7 +84,9 @@ class FetchExport:
parameterType="Required",
direction="Input",
)

add_to_map.value = True

return [grid_server, grid_access_token, aoi_name, dl_directory, add_to_map]

def updateParameters(self, parameters):
Expand All @@ -89,37 +98,46 @@ class FetchExport:
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
if parameters[0].value not in [
"https://grid.nga.mil/grid",
"https://grid.nga.smil.mil",
"https://grid.nga.ic.gov",
]:
parameters[0].clearMessage()
return

def execute(self, parameters, messages):
"""The source code of the tool."""

named_parameters = SyncParameters(
*parameters
)
named_parameters = SyncParameters(*parameters)

token = named_parameters.token.valueAsText
url = named_parameters.grid_server.valueAsText
log_level = "DEBUG"

arcpy.AddMessage("====== PARAMETERS ======")
for parameter_print in named_parameters:
arcpy.AddMessage(
f"{parameter_print.displayName} = {parameter_print.valueAsText}"
)

# no event loop in ESRI toolboxes?
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

aoi_url = named_parameters.aoi_pk.valueAsText
aoi_pk = aoi_url[-7:].strip("/")

arcpy.AddMessage(f"AOI PK: {aoi_pk}")
# for some reason it displays it with the first char missing
# arcpy.AddMessage(f"AOI PK: {aoi_pk}")

output_dir = os.fsdecode(
named_parameters.directory.valueAsText
).replace(os.sep, "/")
output_dir = os.fsdecode(named_parameters.directory.valueAsText).replace(
os.sep, "/"
)

arcpy.AddMessage(f"Saving to {output_dir}")

arcpy.AddMessage(
f"Add to map: {named_parameters.add_to_map.value}"
)
arcpy.AddMessage(f"Add to map: {named_parameters.add_to_map.value}")

app = Application(
token,
Expand All @@ -132,27 +150,40 @@ class FetchExport:
)
contents = asyncio.run(sync(app, aoi_pk))

files_to_render = [
os.fsdecode(content.target)
for content in contents
if hasattr(content, 'target') and isinstance(content.target, pathlib.Path)
]
# Looking for error with invalid server URL
try:
sync(app, aoi_pk)
except AttributeError:
arcpy.AddError(
f"Cannot connect to server. Server URL {url} may not be valid."
)
except RuntimeError:
arcpy.AddError(
f"Unauthorized request. GRiD Access Token or AOI Key: '{aoi_pk}' may not be valid."
)
else:
aprx = arcpy.mp.ArcGISProject("CURRENT")
active_map = aprx.activeMap
arcpy.env.addOutputsToMap = True
if active_map is None:
arcpy.AddMessage("Active Map is None")

elif named_parameters.add_to_map.value:

files_to_render = [
os.fsdecode(content.target)
for content in contents
if hasattr(content, 'target') and isinstance(content.target, pathlib.Path)
]

for f in files_to_render:
try:
active_map.addDataFromPath(f)
arcpy.AddMessage(f"{f} added to map.")
except RuntimeError:
arcpy.AddWarning(f"{f} cannot be added to the map directly.")
# thing to note: gpkg files can't get added this way, so that might be another TODO

aprx = arcpy.mp.ArcGISProject("CURRENT")
active_map = aprx.activeMap
arcpy.env.addOutputsToMap = True
if active_map is None:
arcpy.AddMessage("Cannot add files to active map as there is no active map.")

elif named_parameters.add_to_map.value:
for f in files_to_render:
try:
active_map.addDataFromPath(f)
arcpy.AddMessage(f"{f} added to map.")
except RuntimeError:
arcpy.AddWarning(
f"{f} cannot be added to the map."
)
return None

def postExecute(self, parameters):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata xml:lang="en"><Esri><CreaDate>20230512</CreaDate><CreaTime>14062800</CreaTime><ArcGISFormat>1.0</ArcGISFormat><SyncOnce>TRUE</SyncOnce><ModDate>20230512</ModDate><ModTime>140628</ModTime></Esri><toolbox name="doppkit_wrapper_toolbox" alias="grid access"><arcToolboxHelpPath>c:\users\ogi\appdata\local\programs\arcgis\pro\Resources\Help\gp</arcToolboxHelpPath><toolsets/></toolbox><dataIdInfo><idCitation><resTitle>doppkit_wrapper_toolbox</resTitle></idCitation></dataIdInfo><distInfo><distributor><distorFormat><formatName>ArcToolbox Toolbox</formatName></distorFormat></distributor></distInfo></metadata>
<metadata xml:lang="en"><Esri><CreaDate>20230512</CreaDate><CreaTime>14062800</CreaTime><ArcGISFormat>1.0</ArcGISFormat><SyncOnce>TRUE</SyncOnce><ModDate>20230621</ModDate><ModTime>154806</ModTime></Esri><toolbox name="doppkit_wrapper_toolbox" alias="grid access"><arcToolboxHelpPath>c:\program files\arcgis\pro\Resources\Help\gp</arcToolboxHelpPath><toolsets/></toolbox><dataIdInfo><idCitation><resTitle>doppkit_wrapper_toolbox</resTitle></idCitation></dataIdInfo><distInfo><distributor><distorFormat><formatName>ArcToolbox Toolbox</formatName></distorFormat></distributor></distInfo></metadata>

0 comments on commit 872c5bf

Please sign in to comment.