Skip to content

Commit

Permalink
#7 add resolution and scale readers
Browse files Browse the repository at this point in the history
  • Loading branch information
leodarengosse committed Jan 17, 2019
1 parent 108d0a8 commit 5ae6165
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 71 deletions.
86 changes: 33 additions & 53 deletions isogeo_xml_toolbelt/reader_iso19139.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ def __init__(self, xml: Path):
# vector or raster
self.storageType = utils.xmlGetTextTag(
self.md,
"/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:spatialRepresentationType/gmd:MD_SpatialRepresentationTypeCode/text()",
self.namespaces)
"/gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:spatialRepresentationType/gmd:MD_SpatialRepresentationTypeCode",
self.namespaces,
"codeListValue")

# format
self.formatName = utils.xmlGetTextNodes(
Expand Down Expand Up @@ -197,8 +198,33 @@ def __init__(self, xml: Path):
self.geometry = utils.xmlGetTextTag(
self.md,
"gmd:spatialRepresentationInfo/gmd:MD_VectorSpatialRepresentation/"
"gmd:geometricObjects/gmd:MD_GeometricObjects/gmd:geometricObjectType/gmd:MD_GeometricObjectTypeCode/text()",
self.namespaces)
"gmd:geometricObjects/gmd:MD_GeometricObjects/gmd:geometricObjectType/gmd:MD_GeometricObjectTypeCode",
self.namespaces,
"codeListValue")

#resolution for rasters
try:
self.resolution = int(utils.xmlGetTextNodes(
self.md,
"/gmd:MD_Metadata/gmd:identificationInfo/"
"gmd:MD_DataIdentification/gmd:spatialResolution/"
"gmd:MD_Resolution/gmd:distance/gco:Distance/text()",
self.namespaces))
except:
self.resolution = None

#scale
try:
self.scale = int(utils.xmlGetTextNodes(
self.md,
"/gmd:MD_Metadata/gmd:identificationInfo/"
"gmd:MD_DataIdentification/gmd:spatialResolution/"
"gmd:MD_Resolution/gmd:equivalentScale/gmd:MD_RepresentativeFraction/"
"gmd:denominator/gco:Integer/text()",
self.namespaces))
except:
self.scale = None


# SRS
self.srs_code = utils.xmlGetTextNodes(
Expand Down Expand Up @@ -248,54 +274,6 @@ def get_md_contacts(self) -> dict:
md_contact.append(Contact(pct, self.namespaces).asDict())

return md_contact
# try:
# name = contact.find("gmd:individualName/gco:CharacterString", self.namespaces).text
# except:
# name = None
# try:
# organisation = contact.find("gmd:organisationName/gco:CharacterString", self.namespaces).text
# except:
# organisation = None

# adr_path = "gmd:contactInfo/gmd:CI_Contact/gmd:address/gmd:CI_Address/"

# try:
# rue = contact.find(adr_path + "gmd:deliveryPoint/gco:CharacterString", self.namespaces).text
# except:
# rue = None
# try:
# ville = contact.find(adr_path + "gmd:city/gco:CharacterString", self.namespaces).text
# except:
# ville = None
# try:
# cp = contact.find(adr_path + "gmd:postalCode/gco:CharacterString", self.namespaces).text
# except:
# cp = None
# try:
# country = contact.find(adr_path + "gmd:country/gco:CharacterString", self.namespaces).text
# except:
# country = None
# try:
# mail = contact.find(adr_path + "gmd:electronicMailAddress/gco:CharacterString", self.namespaces).text
# except:
# mail = None

# try:
# telephone = contact.find("gmd:contactInfo/gmd:CI_Contact/"
# "gmd:phone/gmd:CI_Telephone/gmd:voice/gco:CharacterString", self.namespaces).text
# except:
# telephone = None

# try:
# role = contact.find("gmd:role/gmd:CI_RoleCode", self.namespaces).get("codeListValue")
# except:
# role = None

# md_contact.append({"name": name,"organisation": organisation,"role": role,"rue": rue,"ville": ville,
# "cp": cp,"country": country,"mail": mail,"telephone": telephone})

# return md_contact


def get_md_keywords(self) -> list:

Expand Down Expand Up @@ -338,6 +316,8 @@ def asDict(self) -> dict:
"md_date": self.md_date,
"date": self.date,
"geometry": self.geometry,
"resolution": self.resolution,
"scale": self.scale,
"srs": "{}:{}".format(self.srs_codeSpace, self.srs_code),
"latmin": self.latmin,
"latmax": self.latmax,
Expand All @@ -362,6 +342,6 @@ def asDict(self) -> dict:
li_fixtures_xml = sorted(Path(r"input").glob("**/*.xml"))
for xml_path in li_fixtures_xml:
test = MetadataIso19139(xml=xml_path)
print(test.asDict().get("title"), test.asDict().get("keywords"))
print(test.asDict().get("title"), test.asDict().get("scale"))
# print(test.asDict())
# print(xml_path.resolve(), test.storageType)
5 changes: 3 additions & 2 deletions isogeo_xml_toolbelt/switch_from_geosource.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# required subfolders
input_dir = Path("input/").mkdir(exist_ok=True)
input_dir = Path("output/").mkdir(exist_ok=True)
output_dir = Path("output/").mkdir(exist_ok=True)

# logging
logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -178,7 +178,7 @@ def get_metadata(metadata_path: str, metadata_type: str="iso19139") -> tuple:
help="Path to the input folder. Default: './input'.")
@click.option("--output_dir", default=r"output",
help="Path to the output folder. Default: './output'.")
@click.option("--csv", default=1, help="Summarize into a CSV file. Default: True.")
# @click.option("--csv", default=1, help="Summarize into a CSV file. Default: True.")
def cli_switch_from_geosource(input_dir, output_dir):
input_folder = Path(input_dir)
if not input_folder.exists():
Expand Down Expand Up @@ -207,6 +207,7 @@ def cli_switch_from_geosource(input_dir, output_dir):
dest_dir.mkdir(parents=True, exist_ok=True)
# format output filename
md = get_metadata(d_metadata.get(i)[1], d_metadata.get(i)[2])
# print(md)
if not md:
continue
md_title = md.get("title")
Expand Down
25 changes: 9 additions & 16 deletions isogeo_xml_toolbelt/xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import arrow
from lxml import etree


# #############################################################################
# ########## Globals ###############
# ##################################
Expand Down Expand Up @@ -50,30 +51,21 @@ def xmlGetTextNodes(self, doc: etree._ElementTree, xpath: str, namespaces: dict)
"""
return ", ".join(doc.xpath(xpath, namespaces=namespaces))

def xmlGetTextTag(self, doc: etree._ElementTree, xpath: str, namespaces: dict):
def xmlGetTextTag(self, doc: etree._ElementTree, xpath: str, namespaces: dict, key: str):

"""Function to get information in tag when information isn't in nodes matching a specific xpath.
:param lxml.etree._ElementTree doc: XML element to parse
:param str xpath: Xpath to reach
:param dict namespaces: XML namespaces like 'lxml.etree.getroot().nsmap'
:param key : XML key to find like 'codeListValue'
"""
#XML Isogeo example: <MD_GeometricObjectTypeCode codeList="http://...#MD_GeometricObjectTypeCode" codeListValue="surface">surface</MD_GeometricObjectTypeCode>

tag = self.xmlGetTextNodes(
doc,
xpath,
namespaces)

#XML GeoSource example: <gmd:MD_GeometricObjectTypeCode codeList="http://...#MD_GeometricObjectTypeCode" codeListValue="surface" />

if len(tag) < 1:
xpath = xpath.replace("/text()"," ")
tag = doc.xpath(xpath, namespaces=namespaces)
if len(tag) > 0:
tag = tag[0].get("codeListValue", None)
else:
tag = "None"
tag = doc.xpath(xpath, namespaces=namespaces)
if len(tag) > 0:
tag = tag[0].get(key, None)
else:
tag = "None"

return tag

Expand All @@ -96,6 +88,7 @@ def parse_string_for_max_date(self, dates_as_str: str):
logging.error("Date parsing error: " + dates_as_str)
return None



# #############################################################################
# ### Stand alone execution #######
Expand Down

0 comments on commit 5ae6165

Please sign in to comment.