Skip to content

Commit

Permalink
remove XML metadata upload functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Jun 3, 2012
1 parent c2c3ae8 commit 00d3e17
Show file tree
Hide file tree
Showing 12 changed files with 10 additions and 594 deletions.
191 changes: 2 additions & 189 deletions src/GeoNodePy/geonode/catalogue/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,8 @@ def create_from_layer(self, layer):
# all other privileges are set to False for all
# groups.
self.set_metadata_privs(layer.uuid, {"all": {"view": True}})

return "%s?%s" % (self.url, urllib.urlencode({
"request": "GetRecordById",
"service": "CSW",
"version": "2.0.2",
"outputschema": "http://www.isotc211.org/2005/gmd",
"elementsetname": "full",
"id": layer.uuid
}))

return self.url_for_uuid(layer.uuid, namespaces['gmd'])

def delete_layer(self, layer):
response = self.csw_request(layer, "maps/csw/transaction_delete.xml")
Expand Down Expand Up @@ -249,186 +242,6 @@ def normalize_bbox(bbox):
else: # swap coords per standard
return [bbox[1], bbox[0], bbox[3], bbox[2]]

def update_metadata(layer_uuid, xml, saved_layer):
"""Update metadata XML with GeoNode specific information"""
logger.info('>>> Step XML. If an XML metadata document was passed, process it')
# Step XML. If an XML metadata document is uploaded,
# parse the XML metadata and update uuid and URLs as per the content model

# check if document is XML
try:
exml = etree.fromstring(xml)
except Exception, err:
raise GeoNodeException('Uploaded XML document is not XML: %s' % str(err))

# check if document is an accepted XML metadata format
try:
tagname = exml.tag.split('}')[1]
except:
tagname = exml.tag

# update relevant XML
layer_updated = saved_layer.date.strftime('%Y-%m-%dT%H:%M:%SZ')

if catalogue_connection['non_iso_xml_upload_enabled'] is False:
raise GeoNodeException('Only ISO XML is supported')

if tagname == 'Record': # Dublin Core
dc_ns = '{http://purl.org/dc/elements/1.1/}'
dct_ns ='{http://purl.org/dc/terms/}'

children = exml.getchildren()

# set/update identifier
xname = exml.find('%sidentifier' % dc_ns)
if xname is None: # doesn't exist, insert it
value = etree.Element('%sidentifier' % dc_ns)
value.text = layer_uuid
children.insert(0, value)
else: # exists, update it
xname.text = layer_uuid

xname = exml.find('%smodified' % dct_ns)
if xname is None: # doesn't exist, insert it
value = etree.Element('%smodified' % dct_ns)
value.text = layer_updated
children.insert(3, value)
else: # exists, update it
xname.text = layer_updated

# set/update URLs
http_link = etree.Element('%sreferences' % dct_ns, scheme='WWW:LINK-1.0-http--link')
http_link.text = '%s%s' % (settings.SITEURL, saved_layer.get_absolute_url())
children.insert(-2, http_link)

for extension, dformat, link in saved_layer.download_links():
http_link = etree.Element('%sreferences' % dct_ns, scheme='WWW:DOWNLOAD-1.0-http--download')
http_link.text = link
children.insert(-2, http_link)

from owslib.csw import CswRecord
# set django properties
csw_exml = CswRecord(exml)
md_title = csw_exml.title
md_abstract = csw_exml.abstract

elif tagname == 'MD_Metadata':
gmd_ns = 'http://www.isotc211.org/2005/gmd'
gco_ns = 'http://www.isotc211.org/2005/gco'

# set/update gmd:fileIdentifier
xname = exml.find('{%s}fileIdentifier' % gmd_ns)
if xname is None: # doesn't exist, insert it
children = exml.getchildren()
fileid = etree.Element('{%s}fileIdentifier' % gmd_ns)
etree.SubElement(fileid, '{%s}CharacterString' % gco_ns).text = layer_uuid
children.insert(0, fileid)
else: # gmd:fileIdentifier exists, check for gco:CharacterString
value = xname.find('{%s}CharacterString' % gco_ns)
if value is None:
etree.SubElement(xname, '{%s}CharacterString' % gco_ns).text = layer_uuid
else:
value.text = layer_uuid

# set/update gmd:dateStamp
xname = exml.find('{%s}dateStamp' % gmd_ns)
if xname is None: # doesn't exist, insert it
children = exml.getchildren()
datestamp = etree.Element('{%s}dateStamp' % gmd_ns)
etree.SubElement(datestamp, '{%s}DateTime' % gco_ns).text = layer_updated
children.insert(4, datestamp)
else: # gmd:dateStamp exists, check for gco:Date or gco:DateTime
value = xname.find('{%s}Date' % gco_ns)
value2 = xname.find('{%s}DateTime' % gco_ns)

if value is None and value2 is not None: # set gco:DateTime
value2.text = layer_updated
elif value is not None and value2 is None: # set gco:Date
value.text = saved_layer.date.strftime('%Y-%m-%d')
elif value is None and value2 is None:
etree.SubElement(xname, '{%s}DateTime' % gco_ns).text = layer_updated

# set/update URLs
children = exml.getchildren()
distinfo = etree.Element('{%s}distributionInfo' % gmd_ns)
distinfo2 = etree.SubElement(distinfo, '{%s}MD_Distribution' % gmd_ns)
transopts = etree.SubElement(distinfo2, '{%s}transferOptions' % gmd_ns)
transopts2 = etree.SubElement(transopts, '{%s}MD_DigitalTransferOptions' % gmd_ns)

online = etree.SubElement(transopts2, '{%s}onLine' % gmd_ns)
online2 = etree.SubElement(online, '{%s}CI_OnlineResource' % gmd_ns)
linkage = etree.SubElement(online2, '{%s}linkage' % gmd_ns)
etree.SubElement(linkage, '{%s}URL' % gmd_ns).text = '%s%s' % (settings.SITEURL, saved_layer.get_absolute_url())
protocol = etree.SubElement(online2, '{%s}protocol' % gmd_ns)
etree.SubElement(protocol, '{%s}CharacterString' % gco_ns).text = 'WWW:LINK-1.0-http--link'

for extension, dformat, link in saved_layer.download_links():
online = etree.SubElement(transopts2, '{%s}onLine' % gmd_ns)
online2 = etree.SubElement(online, '{%s}CI_OnlineResource' % gmd_ns)
linkage = etree.SubElement(online2, '{%s}linkage' % gmd_ns)

etree.SubElement(linkage, '{%s}URL' % gmd_ns).text = link or ''
protocol = etree.SubElement(online2, '{%s}protocol' % gmd_ns)
etree.SubElement(protocol, '{%s}CharacterString' % gco_ns).text = 'WWW:DOWNLOAD-1.0-http--download'
lname = etree.SubElement(online2, '{%s}name' % gmd_ns)
etree.SubElement(lname, '{%s}CharacterString' % gco_ns).text = extension or ''
ldesc = etree.SubElement(online2, '{%s}description' % gmd_ns)
etree.SubElement(ldesc, '{%s}CharacterString' % gco_ns).text = dformat or ''

children.insert(-2, distinfo)

from owslib.iso import MD_Metadata
iso_exml = MD_Metadata(exml)
md_title = iso_exml.identification.title
md_abstract = iso_exml.identification.abstract

elif tagname == 'metadata': # FGDC
# set/update identifier
xname = exml.find('idinfo/datasetid')
if xname is None: # doesn't exist, insert it
children = exml.find('idinfo').getchildren()
value = etree.Element('datasetid')
value.text = layer_uuid
children.insert(0, value)
else: # exists, update it
xname.text = layer_uuid

xname = exml.find('metainfo/metd')
if xname is None: # doesn't exist, insert it
value = etree.Element('metd')
value.text = layer_updated
exml.find('metainfo').append(value)
else: # exists, update it
xname.text = layer_updated

# set/update URLs
citeinfo = exml.find('idinfo/citation/citeinfo')
http_link = etree.Element('onlink')
http_link.attrib['type'] = 'WWW:LINK-1.0-http--link'
http_link.text = '%s%s' % (settings.SITEURL, saved_layer.get_absolute_url())
citeinfo.append(http_link)

for extension, dformat, link in saved_layer.download_links():
http_link = etree.Element('onlink')
http_link.attrib['type'] = 'WWW:DOWNLOAD-1.0-http--download'
http_link.text = link
citeinfo.append(http_link)

from owslib.fgdc import Metadata as FGDC_Metadata


with open('/tmp/fff.txt','w') as ff:
ff.write(str(etree.tostring(exml)))

fgdc_exml = FGDC_Metadata(exml)
md_title = fgdc_exml.idinfo.citation.citeinfo['title']
md_abstract = fgdc_exml.idinfo.descript.abstract

else:
raise GeoNodeException('Unsupported metadata format')

return [etree.tostring(exml), md_title, md_abstract]

def metadatarecord2dict(rec, catalogue):
"""
accepts a node representing a catalogue result
Expand Down
14 changes: 2 additions & 12 deletions src/GeoNodePy/geonode/maps/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class LayerUploadForm(forms.Form):
dbf_file = forms.FileField(required=False)
shx_file = forms.FileField(required=False)
prj_file = forms.FileField(required=False)
metadata_file = forms.FileField(required=False)

spatial_files = ("base_file", "dbf_file", "shx_file", "prj_file", "metadata_file")
spatial_files = ("base_file", "dbf_file", "shx_file", "prj_file")

def clean(self):
cleaned = super(LayerUploadForm, self).clean()
Expand All @@ -43,12 +42,6 @@ def clean(self):
raise forms.ValidationError("It looks like you're "
"uploading components from different Shapefiles. "
"Please double-check your file selections.")
if cleaned["metadata_file"] is not None:
metadata_file = cleaned["metadata_file"].name
if os.path.splitext(metadata_file)[0] != base_name:
if metadata_file.find('.shp') != -1:
# force rename of file so that file.shp.xml doesn't overwrite as file.shp
cleaned["metadata_file"].name = '%s.xml' % base_name
return cleaned

def write_files(self):
Expand All @@ -71,7 +64,4 @@ class NewLayerUploadForm(LayerUploadForm):
layer_title = forms.CharField(required=False)
permissions = JSONField()

spatial_files = ("base_file", "dbf_file", "shx_file", "prj_file", "sld_file", "metadata_file")

class LayerMetadataUploadForm(forms.Form):
metadata_file = forms.FileField(required=True)
spatial_files = ("base_file", "dbf_file", "shx_file", "prj_file", "sld_file")
Loading

0 comments on commit 00d3e17

Please sign in to comment.