Part 1: General Cleanup
(Edit: Part 2 is here.)
I have a script in a Work Order Management System (Maximo) that performs a spatial query.
Details:
- Takes the X&Y coordinates of a work order in Maximo
- Generates a URL from the coordinates (including other spatial query information)
- Executes the URL via a separate script/library (LIB_HTTPCLIENT)
- Performs a spatial query in an ESRI REST feature service (a separate GIS system)
- JSON text is returned to Maximo with the attributes of the zone that the work order intersected
- The zone number is parsed from the JSON text via a separate script/library (LIB_PARSE_JSON)
- Inserts the zone number into the work order record
Jython main automation script
from psdi.mbo import MboConstants
from java.util import HashMap
#Get the y and x coordinates(UTM projection) from the WOSERVICEADDRESS table via the SERVICEADDRESS system relationship
#The datatype of the LatitydeY and LongitudeX is decimal: 1234567.8335815760, 123456.4075621164
#Convert the decimals to integers, and then strings, for the purpose of generating the URL (I don't think the URL can have decimal places)
laty = str(mbo.getInt("SERVICEADDRESS.LatitudeY"))
longx = str(mbo.getInt("SERVICEADDRESS.LONGITUDEX"))
#Verify if the numbers are legitimate UTM coordinates
if len(laty) == 7 and len(longx) == 6:
#Assemble the URL (including the longx and the laty). Note: The coordinates are flipped in the url
url="http://something.com/arcgis/rest/services/something/Zones/MapServer/15/query?geometry=" + longx + "%2C" + laty + "&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson"
#Get the JSON text from the feature service (the JSON text contains the zone value)
ctx = HashMap()
ctx.put("url",url)
service.invokeScript("LIB_HTTPCLIENT",ctx)
json_text = str(ctx.get("response"))
#Parse the zone value from the JSON text
ctx = HashMap()
ctx.put("json_text",json_text)
service.invokeScript("LIB_PARSE_JSON",ctx)
parsed_val = str(ctx.get("parsed_val"))
#Enter the zone value into the zone field in the work order
mbo.setValue("DESCRIPTION","Waste Zone: "+parsed_val,MboConstants.NOACCESSCHECK)
Jython library (LIB_HTTPCLIENT)
from psdi.iface.router import HTTPHandler
from java.util import HashMap
from java.lang import String
handler = HTTPHandler()
map = HashMap()
map.put("URL",url)
map.put("HTTPMETHOD","GET")
responseBytes = handler.invoke(map,None)
response = String(responseBytes,"utf-8")
JavaScript library (LIB_PARSE_JSON)
#The field name (ZONE) is hardcoded. I'm not sure if this is best practice or not.
var obj = JSON.parse(json_text);
parsed_val = obj.features[0].attributes.ZONE
How can this code be improved?