Skip to content

Instantly share code, notes, and snippets.

@mhogeweg
Created August 20, 2018 16:30
Show Gist options
  • Save mhogeweg/95717823010fd5149b9be7312e4454ff to your computer and use it in GitHub Desktop.
Save mhogeweg/95717823010fd5149b9be7312e4454ff to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import httplib
from base64 import b64encode
import urllib2
import sys
import datetime
import time
import json
import arcpy
# The URL for the geoportal's document management API.
connection = httplib.HTTPConnection('<GEOPORTALSERVERDOMAIN>')
userAndPass = b64encode(b"<ADMINUSERNAME>:<ADMINPASSWORD>").decode("ascii")
headers = {'Authorization': 'Basic %s' % userAndPass, 'Content-type': 'application/xml'}
log = sys.stdout
site_root = "https://<YOURDOMAINGOESHERE>/arcgis/rest/services/<FOLDER>/<SERVICENAME>/ImageServer"
count = 0
srFrom = arcpy.SpatialReference(3857)
srTo = arcpy.SpatialReference(4326)
def crawl(page):
for i in range(1, 10):
the_url = page + "/" + str(i)
f = urllib2.urlopen(the_url + "?f=json") # it's a file like object
body_content = f.read()
print body_content
body_json = json.loads(body_content)
# body_json = {"error": {"code": 400, "message": "Unable to complete operation.", "details": []}}
point = arcpy.Point()
point.X = body_json['attributes']['CenterX']
point.Y = body_json['attributes']['CenterY']
point_geometry = arcpy.PointGeometry(point, srFrom)
projected_point = point_geometry.projectAs(srTo)
acquisition_date = body_json['attributes']['AcquisitionDate']
# print str(acquisition_date)
local_time = time.localtime(acquisition_date/1000)
# print str(local_time)
formatted_time = time.strftime('%Y%m%d', local_time)
body_xml_pre = """<?xml version="1.0" encoding="UTF-8"?><metadata>
<idinfo>
<citation>
<citeinfo>
<origin>Esri</origin>
<pubdate>20160916</pubdate>
<title>""" + body_json['attributes']['GroupName'] + ' - ' + body_json['attributes']['Name'] + """</title>
<onlink>""" + the_url + """</onlink>
</citeinfo>
</citation>
<descript>
<abstract>""" + body_json['attributes']['GroupName'] + ' - ' + body_json['attributes']['Name'] + """</abstract>
<purpose>Supports all ArcGIS Raster Datasets</purpose>
</descript>
<timeperd>
<timeinfo>
<mdattim>
<sngdate>
<caldate>""" + formatted_time + """</caldate>
</sngdate>
<sngdate>
<caldate>Unknown</caldate>
</sngdate>
</mdattim>
</timeinfo>
<current>publication date</current>
</timeperd>
<status>
<progress>In work</progress>
<update>Daily</update>
</status>
<spdom>
<bounding>
<westbc>""" + str(projected_point.firstPoint.X - 0.5) + """</westbc>
<eastbc>""" + str(projected_point.firstPoint.X + 0.5) + """</eastbc>
<northbc>""" + str(projected_point.firstPoint.Y - 0.5) + """</northbc>
<southbc>""" + str(projected_point.firstPoint.Y + 0.5) + """</southbc>
</bounding>
</spdom>
<keywords>"""
body_xml_post = """</keywords>
<accconst>None</accconst>
<useconst>None</useconst>
</idinfo>
<metainfo>
<metd></metd>
<metc>
<cntinfo>
<cntorgp>
<cntorg></cntorg>
</cntorgp>
<cntaddr>
<addrtype></addrtype>
<city></city>
<state></state>
<postal></postal>
</cntaddr>
<cntvoice></cntvoice>
<cntemail></cntemail>
</cntinfo>
</metc>
<metstdn>FGDC Content Standard for Digital Geospatial Metadata</metstdn>
<metstdv>FGDC-STD-001-1998</metstdv>
</metainfo>
</metadata>"""
fields = ['OBJECTID', 'Name', 'MinPS', 'MaxPS', 'LowPS', 'HighPS', 'Category', 'GroupName', 'ProductName', 'SunAzimuth', 'SunElevation', 'WRS_Path', 'WRS_Row', 'Best', 'AcquisitionDate', 'DateUpdated', 'SunAzimuth', 'SunElevation', 'CloudCover', 'PR', 'Latest', 'DayOfYear', 'Month', 'SensorName', 'dataset_id']
body_xml_info = ""
for field in fields:
value = body_json['attributes'][field]
if field == 'CloudCover':
value = 100 * value
body_xml_info += "<theme><themekt>" + field + "</themekt><themekey>" + str(
value) + "</themekey></theme>"
body_xml = body_xml_pre + body_xml_info + body_xml_post
connection.request('PUT', '/<GEOPORTALCONTEXTNAME>/rest/metadata/item', body_xml, headers)
result = connection.getresponse()
result.read()
log.write('%s %s\n' % (result.status, the_url))
f.close()
def main():
start = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
crawl(site_root)
end = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log.write('Start %s\n' % start)
log.write('End %s\n' % end)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment