def geocodeAddress(address): data = {'Single Line Input': address, 'f': 'json', 'outSR': 4326} url = 'http://parcels.downers.us/arcgis/rest/services/Locators/LAddress/GeocodeServer/findAddressCandidates' results = dghttpclient.getHttpRequest(url, data) if 'candidates' in results and len(results['candidates']) > 0: return float(results['candidates'][0]['location']['x']), float( results['candidates'][0]['location']['y']) else: return 0, 0
def getEmployeeEmailName(d): if d['assignEmp'] is None: d['assignEmp'] = '9999' data = {'where': 'EmployID = ' + str(d['assignEmp']), 'outFields': '*'} url = mapURL + requestDataURL['Employees'] + '/query' email = dghttpclient.getHttpRequest(url, data)['features'] if len(email) > 0: email = email[0]['attributes']['Email'] else: email = '*****@*****.**' d['assignEmp'] = '9999' d['assignEmpText'] = 'Unassigned' d['assignEmpDept'] = '9999' d['assignEmpDeptText'] = 'Unassigned' return email, d data['where'] = 'EMP_NO = ' + str(d['assignEmp']) url = mapURL + requestDataURL['EmployeesEden'] + '/query' name = dghttpclient.getHttpRequest(url, data)['features'] if len(name) > 0: d['assignEmpText'] = name[0]['attributes']['FULL_NAME'] d['assignEmpDept'] = name[0]['attributes']['DEPT_NO'] d['assignEmpDeptText'] = name[0]['attributes']['DEPT'] return email, d
def checkForAdditionalInfo(requestText): url = mapURL + requestDataURL['Types'] + '/query' data = { 'where': 'RequestName = \'' + requestText + '\'', 'outFields': 'RequestID', 'f': 'json' } requestType = dghttpclient.getHttpRequest(url, data) if 'features' in requestType and len(requestType['features']) > 0: reqID = requestType['features'][0]['attributes']['RequestID'] else: return False url = mapURL + requestDataURL['Questions'] + '/query' data = { 'where': 'RequestTypeID = ' + str(reqID), 'returnCountOnly': True, 'f': 'json' } additionalCount = dghttpclient.getHttpRequest(url, data) if 'count' in additionalCount and additionalCount['count'] > 0: return True else: return False
def getRequestData(params): #url = params.getfirst('u') url = mapURL + requestDataURL['Requests'] + '/query' if queryType == 'requester': data = getRequesterParameters(params) else: data = parseParameters(params) if 'd' in params: days = params.getValue('d') # Do additional parsing result = dghttpclient.getHttpRequest(url, data) parseAndGetAdditionalData(result)
def checkStreetLight(d, count): delta = .0005 * count LocationX = d['x'] LocationY = d['y'] LocationXMin = float(LocationX) - delta LocationYMin = float(LocationY) - delta LocationXMax = float(LocationX) + delta LocationYMax = float(LocationY) + delta streetLightUrl = 'http://parcels.downers.us/arcgis/rest/services/PSRT/MapServer/0/query' streetLightData = { 'f': 'json', 'geometry': str(LocationXMin) + ',' + str(LocationYMin) + ',' + str(LocationXMax) + ',' + str(LocationYMax), 'geometryType': 'esriGeometryEnvelope', 'inSR': 4326, 'spatialRel': 'esriSpatialRelContains', 'outFields': '*', 'returnGeometry': 'false' } result = dghttpclient.getHttpRequest(streetLightUrl, streetLightData) features = result['features'] global sl sl = {'AssetType': 'Street Light'} if len(features) > 0: for feature in features: attrs = feature['attributes'] owner = attrs['Owner'] sl['AssetOwner'] = owner if owner == 'Village': sl['AssetID'] = str(attrs.get('SystemID')) + "-" + str( attrs.get('PoleID')) break else: sl['AssetID'] = str(attrs.get('Address')) else: if count < 4: checkStreetLight(d, count + 1) else: sl['AssetOwner'] = 'Unknown' sl['AssetID'] = 'Unknown' return sl
def jurisdictionRightOfWay(x, y): data = { 'f': 'json', 'geometry': str(x) + ',' + str(y), 'geometryType': 'esriGeometryPoint', 'inSR': 4326, 'spatialRel': 'esriSpatialRelIntersects', 'outFields': '*', 'returnGeometry': 'false' } url = mapURL + requestDataURL['ROW'] + '/query' juris = dghttpclient.getHttpRequest(url, data)['features'] if len(juris) > 0: juris = juris[0]['attributes']['MAINT'] else: juris = 'VDG' return juris
def geoQuery(geo, geoType, service, queryType, returnGeo): data = { 'f': 'json', 'geometry': geo, 'geometryType': geoType, 'inSR': 4326, 'outSR': 4326, 'spatialRel': queryType, 'outFields': '*', 'returnGeometry': returnGeo } url = baseURL + service + '/query' """if geoType == 'esriGeometryPolygon': print url print geoType,queryType,returnGeo print geo""" return dghttpclient.getHttpRequest(url, data)['features']
def checkVillageBoundary(x, y): url = 'http://parcels.downers.us/arcgis/rest/services/Public/Requests311/MapServer/2/query' geo = str(x) + ',' + str(y) data = { 'f': 'json', 'geometry': geo, 'geometryType': 'esriGeometryPoint', 'inSR': 4326, 'spatialRel': 'esriSpatialRelIntersects', 'outFields': '*', 'returnGeometry': 'false' } data['where'] = 'Type = \'Boundary\'' village = dghttpclient.getHttpRequest(url, data)['features'] if len(village) > 0: village = village[0]['attributes']['EmployName'] else: village = 'Out' return village
def routeViaGeo(geo, geoType, where): url = mapURL + requestDataURL['Response'] + '/query' data = { 'f': 'json', 'geometry': geo, 'geometryType': geoType, 'inSR': 4326, 'spatialRel': 'esriSpatialRelIntersects', 'outFields': '*', 'returnGeometry': 'false' } if where == 'Streets': where = 'PW-TECH' data['where'] = 'Type = \'' + where + '\'' emp = dghttpclient.getHttpRequest(url, data)['features'] if len(emp) > 0: emp = emp[0]['attributes']['EmployID'] else: emp = None return emp
def createRequest(d): global requestID count = 1 index = 0 while (count > 0 or index < 10): index += 1 requestID = nowDate.strftime('%Y%m%d-%H%M') + str(randint(10, 99)) url = mapURL + requestDataURL['Requests'] + '/query' data = { 'where': 'RequestID = \'' + requestID + '\'', 'returnCountOnly': 'true', 'f': 'json' } count = dghttpclient.getHttpRequest(url, data) count = count['count'] #print "Create Request" requestUrl = featureURL + requestDataURL['Requests'] + '/addFeatures' requestDict = {'f': 'json', 'gdbVersion': 'GISUSER.Requests'} geometryDict = {'x': d['x'], 'y': d['y']} statusText = 'New Request' statusCode = 2 owner = checkVillageBoundary(d['x'], d['y']) if owner == 'VDG': d, owner, email = routeRequest(d, requestID) calculatedResult['requestType'] = d['RequestTypeText'] if owner != 'VDG': statusCode = 11 statusText = statusCodes[str(statusCode)] calculatedResult['owner'] = outJuris[owner]['name'] calculatedResult['phone'] = outJuris[owner]['phone'] calculatedResult['uri'] = outJuris[owner]['uri'] attributesDict = { 'RequestID': requestID, 'RequestTypeText': d['RequestTypeText'], 'Address': d['Location'], 'Description': d['Description'], 'SubmittedDate': stringFullDate, 'StatusCode': statusCode, 'StatusText': statusText, 'StatusDate': stringFullDate, 'EmployeeID': int(d['assignEmp']), 'EmployeeText': str(d['assignEmpText']), 'DeptID': d['assignEmpDept'], 'DeptText': str(d['assignEmpDeptText']), 'RequestedDate': stringFullDate } attributesDict['UpdatedBy'] = "*****@*****.**" i = "Intersection of " b = "block of " # Need to parse out the address # and street name to help with sorting on the list tab # if location address is an intersection if i in d['Location']: attributesDict['Address'] = d['Location'][len(i):len(i) + 100] attributesDict['AddressNo'] = 'None' attributesDict['StreetName'] = d['Location'][len(i):len(i) + 100] # ... a block address elif b in d['Location']: p = d['Location'].index(' ') attributesDict['AddressNo'] = d['Location'][0:p] attributesDict['StreetName'] = d['Location'][p + len(b) + 1:] attributesDict['Address'] = d['Location'] # ... a normal address else: p = d['Location'].index(' ') attributesDict['Address'] = d['Location'] attributesDict['AddressNo'] = d['Location'][0:p] attributesDict['StreetName'] = d['Location'][p + 1:] attributesDict = checkForNone(attributesDict) featureArray = [{ 'geometry': geometryDict, 'attributes': attributesDict }] requestDict['features'] = featureArray #result = dghttpclient.postHttpRequest(requestUrl,requestDict) result = dghttpclient.postHttpRequestESRI(requestUrl, requestDict) result = result['addResults'] return checkSuccess(result)
def routeRequest(rd, rid): # Get GeoRouteType to determine type of routing needed # Null = Village Boundary Check # Juris = Jurisdiction to determine if we're the responding organization geographically # Staff = Staff boundary has been established for who responds to the request (i.e. Code) url = mapURL + requestDataURL['Types'] + '/query' data = { 'where': 'RequestName = \'' + rd['RequestTypeText'] + '\'', 'f': 'json', 'outFields': '*' } reqType = dghttpclient.getHttpRequest( url, data)['features'][0]['attributes'] jk = '*****@*****.**' email = jk owner = 'VDG' if reqType['GeoRouteType'] == 'Juris' or reqType[ 'GeoRouteType'] == 'Juris|Staff': if rd['RequestTypeText'] == 'Street Light': streetLight = checkStreetLight(rd, 0) rd['asset'] = streetLight addFeature(streetLight) owner = streetLight['AssetOwner'] if owner == 'Village': owner = 'VDG' else: if rd['Location'].find('Intersection') == -1 and rd[ 'Location'].find('Block') == -1: #geoQuery(geo,geoType,service,queryType): g = "{0},{1}".format(rd['x'], rd['y']) parcel = geoQuery(g, 'esriGeometryPoint', 'DGspa/MapServer/2', 'esriSpatialRelIntersects', 'true') owner = 'VDG' if len(parcel) > 0: parcel = parcel[0]['geometry'] xValues = [] yValues = [] for i in range(0, len(parcel['rings'][0])): xValues.append(parcel['rings'][0][i][0]) yValues.append(parcel['rings'][0][i][1]) envelope = { "xmax": max(xValues), "ymax": max(yValues), "xmin": min(xValues), "ymin": min(yValues), "spatialReference": { "wkid": 4326 } } row = geoQuery( envelope, 'esriGeometryEnvelope', 'Public/Requests311/MapServer/' + requestDataURL['ROW'], 'esriSpatialRelIntersects', 'false') owner = [] for r in row: owner.append(r['attributes']['MAINT']) if 'VDG' in owner: owner = 'VDG' else: if len(owner) == 0: owner = 'Out' else: owner = owner[0] else: owner = jurisdictionRightOfWay(rd['x'], rd['y']) # Get default or first responding employee before routing if 'assignEmp' not in rd or ('assignEmp' in rd and rd['assignEmp'] == unassignedNum and rd['RequestTypeText'] != 'General'): if reqType['DefaultAssigned'] is None: if reqType['GeoRouteType'] == 'Staff' or reqType[ 'GeoRouteType'] == 'Juris|Staff': rd['assignEmp'] = unassignedNum else: respond = str(reqType['RespondingEmployees']) if respond.find(',') != -1: respond = respond.split(',') if len(respond) > 0: rd['assignEmp'] = respond[0] else: respond = None else: rd['assignEmp'] = respond if respond is None: rd['assignEmp'] = unassignedNum else: rd['assignEmp'] = reqType['DefaultAssigned'] # Get Email before Routing can change it if owner == 'VDG' and ('assignEmp' in rd and (rd['assignEmp'] != unassignedNum and rd['assignEmp'] is not None and rd['assignEmp'] != 'None')): email, rd = getEmployeeEmailName(rd) else: rd['assignEmp'] = unassignedNum rd['assignEmpText'] = unassigned email = jk if owner == 'VDG' and (reqType['GeoRouteType'] == 'Staff' or reqType['GeoRouteType'] == 'Juris|Staff'): # if email is the default (which is me right now) check to see if it should be routed if email == jk: cat = reqType['Category'] if reqType['MapColor'] == '#B1D2BE': cat = 'Code' g = str(rd['x']) + ',' + str(rd['y']) email = routeViaGeo(g, 'esriGeometryPoint', cat) if email is None: email = jk else: rd['assignEmp'] = email email, rd = getEmployeeEmailName(rd) return rd, owner, email
def addContact(d): # 2 Steps -- 1) Create Contact 2) Add relationship to relationship table t = 'contact' if 'cType' in d and d['cType'] is not None: t = d['cType'] contactInfo = (('Email' in d or 'Address' in d or 'Phone' in d or 'Name' in d) and (d['Email'] != 'None' or d['Address'] != 'None' or d['Phone'] != 'None' or d['Name'] != 'None')) if 'UserID' not in d and contactInfo is True: url = mapURL + requestDataURL['Contacts'] + '/query' data = { 'where': 'OBJECTID IS NOT NULL', 'orderByFields': 'ContactID DESC', 'outFields': 'ContactID', 'f': 'json' } uID = dghttpclient.getHttpRequest(url, data) uID = uID['features'][0]['attributes']['ContactID'] uID = uID + 1 url = featureURL + requestDataURL['Contacts'] + '/addFeatures' contact = { 'Type': t, 'Name': d['Name'], 'Address': d['Address'], 'City': 'Downers Grove', 'State': 'IL', 'Zip': '60515', 'Email': d['Email'], 'Phone': d['Phone'], 'Alt_Phone': 'None', 'Notes': 'None', 'ContactID': uID } data = { 'f': 'json', 'features': [{ 'attributes': contact }], 'gdbVersion': 'GISUSER.Requests' } #result = dghttpclient.postHttpRequest(url,data) result = dghttpclient.postHttpRequestESRI(url, data) result = result['addResults'] result = checkSuccess(result) elif 'UserID' in d: uID = d['UserID'] result = True else: return True if (result): url = featureURL + requestDataURL['ContactReqRel'] + '/addFeatures' contactRel = { 'ContactID': str(uID), 'RequestID': str(requestID), 'ContactType': t } data = { 'f': 'json', 'features': [{ 'attributes': contactRel }], 'gdbVersion': 'GISUSER.Requests' } #result = dghttpclient.postHttpRequest(url,data) result = dghttpclient.postHttpRequestESRI(url, data) result = result['addResults'] result = checkSuccess(result) return result else: return False