Search in sources :

Example 1 with Metadata

use of gov.usgs.cida.coastalhazards.xml.model.Metadata in project coastal-hazards by USGS-CIDA.

the class MetadataUtil method getCrsFromFgdcMetadata.

public static CoordinateReferenceSystem getCrsFromFgdcMetadata(String inMetadata) throws FactoryException, JAXBException, UnsupportedEncodingException {
    // create the WKT to instantiate a CRS object from org.geotools.referencing
    CRSParameters crsParms = new CRSParameters();
    Metadata metadata = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Metadata.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        metadata = (Metadata) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(inMetadata.getBytes("UTF-8")));
    } catch (JAXBException e) {
        // schema used https: www.fgdc.gov/schemas/metadata/fgdc-std-001-1998-sect01.xsd
        log.error("Unable to parse xml file. Has the schema changed? https:www.fgdc.gov/schemas/metadata/fgdc-std-001-1998-sect01.xsd :" + e.getMessage());
        throw e;
    }
    Horizsys horizsys = metadata.getSpref().getHorizsys();
    String ellips = horizsys.getGeodetic().getEllips();
    String horizdn = horizsys.getGeodetic().getHorizdn();
    double denflat = horizsys.getGeodetic().getDenflat();
    double semiaxis = horizsys.getGeodetic().getSemiaxis();
    String mapprojn = horizsys.getPlanar().getMapproj().getMapprojn();
    double feast = horizsys.getPlanar().getMapproj().getMapprojp().getFeast();
    double fnorth = horizsys.getPlanar().getMapproj().getMapprojp().getFnorth();
    double latprjo = horizsys.getPlanar().getMapproj().getMapprojp().getLatprjo();
    double longcm = horizsys.getPlanar().getMapproj().getMapprojp().getLongcm();
    double stdparll = horizsys.getPlanar().getMapproj().getMapprojp().getStdparll();
    // these defaults were derived from the first 3 raster files meta-data CR, AE, PAE
    // Hoping that these can be optional or located in future metadata in which case
    // an if check should be performed and the value replaced if it doesn't match the default
    String defaultGcs = "GCS_North_American_1983";
    String defaultPrimeM = "Greenwich\",0.0]";
    String defaultUnit = "Degree\",0.0174532925199433]]";
    String defaultProjection = "Albers";
    String defaultLengthUnit = "Meter";
    double defaultLengthValue = 1.0;
    crsParms.setEllips(ellips);
    crsParms.setHorizdn(horizdn);
    crsParms.setDenflat(denflat);
    crsParms.setSemiaxis(semiaxis);
    crsParms.setMapprojn(mapprojn);
    crsParms.setFeast(feast);
    crsParms.setFnorth(fnorth);
    crsParms.setLatprjo(latprjo);
    crsParms.setLongcm(longcm);
    crsParms.setStdparll(stdparll);
    crsParms.setGcs(defaultGcs);
    crsParms.setPrimeM(defaultPrimeM);
    crsParms.setUnit(defaultUnit);
    crsParms.setProjection(defaultProjection);
    crsParms.setLengthUnit(defaultLengthUnit);
    crsParms.setLengthValue(defaultLengthValue);
    // same as FactoryFinder.getCRSFactory(null).createFromWKT(wkt);
    return CRS.parseWKT(buildWkt(crsParms));
}
Also used : Horizsys(gov.usgs.cida.coastalhazards.xml.model.Horizsys) ByteArrayInputStream(java.io.ByteArrayInputStream) JAXBException(javax.xml.bind.JAXBException) Metadata(gov.usgs.cida.coastalhazards.xml.model.Metadata) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) CRSParameters(gov.usgs.cida.coastalhazards.metadata.CRSParameters)

Example 2 with Metadata

use of gov.usgs.cida.coastalhazards.xml.model.Metadata in project coastal-hazards by USGS-CIDA.

the class MetadataUtilTest method testGetBoundingBoxFromFgdcMetadataCR.

/**
 * Test of getBoundingBoxFromFgdcMetadataCR method, of class MetadataUtil.
 */
@Test
public void testGetBoundingBoxFromFgdcMetadataCR() throws IOException {
    System.out.println("testGetBoundingBoxFromFgdcMetadataPAE");
    // This method tests the parsing that occurs in: Bbox result = MetadataUtil.getBoundingBoxFromFgdcMetadata(metadata); // file is ~40kb
    // spdom is the WGS84 bbox, format for the Bbox is "BOX(%f %f, %f %f)"
    // get the metadata from the test file as a string using this package to locate it ...
    String packageName = this.getClass().getCanonicalName();
    // PackageName: gov.usgs.cida.coastalhazards.rest.data.util.MetadataUtilTest
    System.out.println("PackageName: " + packageName);
    // this is where the test resource is located - gov.usgs.cida.coastalhazards.rest.data + /ne_AEmeta.xml
    String replaced = packageName.replaceAll("[.]", "/");
    String[] names = replaced.split("/util/MetadataUtilTest");
    String packageNameShort = names[0];
    String testFileFullName = packageNameShort + "/" + CRxml;
    String metadataXml = loadResourceAsString(testFileFullName);
    InputStream in = new ByteArrayInputStream(metadataXml.getBytes("UTF-8"));
    Metadata metadata = null;
    // JAXB will require jaxb-api.jar and jaxb-impl.jar part of java 1.6. Much safer way to interrogate xml and maintain than regex
    try {
        // File file = new File(xmlFile);  // FYI: can also be done via file rather than inputStream
        JAXBContext jaxbContext = JAXBContext.newInstance(Metadata.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        metadata = (Metadata) jaxbUnmarshaller.unmarshal(in);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    assertNotNull(metadata);
    Idinfo idinfo = metadata.getIdinfo();
    Spdom spdom = idinfo.getSpdom();
    Bounding bounding = spdom.getBounding();
    double minx = bounding.getWestbc();
    double miny = bounding.getSouthbc();
    double maxx = bounding.getEastbc();
    double maxy = bounding.getNorthbc();
    Bbox result = new Bbox();
    result.setBbox(minx, miny, maxx, maxy);
    System.out.println("Parsed Bbox is: " + result.getBbox());
    Bbox expResult = new Bbox();
    expResult.setBbox("BOX(-77.830618 35.344738, -66.813170 46.642941)");
    assertNotNull(result);
    assertTrue(expResult.getBbox().startsWith("BOX(-77.830618 35."));
    assertTrue(expResult.getBbox().equalsIgnoreCase(result.getBbox()));
}
Also used : Bounding(gov.usgs.cida.coastalhazards.xml.model.Bounding) Spdom(gov.usgs.cida.coastalhazards.xml.model.Spdom) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) Metadata(gov.usgs.cida.coastalhazards.xml.model.Metadata) JAXBContext(javax.xml.bind.JAXBContext) Idinfo(gov.usgs.cida.coastalhazards.xml.model.Idinfo) ByteArrayInputStream(java.io.ByteArrayInputStream) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 3 with Metadata

use of gov.usgs.cida.coastalhazards.xml.model.Metadata in project coastal-hazards by USGS-CIDA.

the class MetadataUtilTest method testGetBoundingBoxFromFgdcMetadataAE.

/**
 * Test of getBoundingBoxFromFgdcMetadataAE method, of class MetadataUtil.
 */
@Test
public void testGetBoundingBoxFromFgdcMetadataAE() throws IOException {
    System.out.println("testGetBoundingBoxFromFgdcMetadataAE");
    // This method tests the parsing that occurs in: Bbox result = MetadataUtil.getBoundingBoxFromFgdcMetadata(metadata); // file is ~40kb
    // spdom is the WGS84 bbox, format for the Bbox is "BOX(%f %f, %f %f)"
    // get the metadata from the test file as a string using this package to locate it ...
    String packageName = this.getClass().getCanonicalName();
    // PackageName: gov.usgs.cida.coastalhazards.rest.data.util.MetadataUtilTest
    System.out.println("PackageName: " + packageName);
    // this is where the test resource is located - gov.usgs.cida.coastalhazards.rest.data + /ne_AEmeta.xml
    String replaced = packageName.replaceAll("[.]", "/");
    String[] names = replaced.split("/util/MetadataUtilTest");
    String packageNameShort = names[0];
    String testFileFullName = packageNameShort + "/" + AExml;
    String metadataXml = loadResourceAsString(testFileFullName);
    InputStream in = new ByteArrayInputStream(metadataXml.getBytes("UTF-8"));
    Metadata metadata = null;
    // JAXB will require jaxb-api.jar and jaxb-impl.jar part of java 1.6. Much safer way to interrogate xml and maintain than regex
    try {
        // File file = new File(xmlFile);  // FYI: can also be done via file rather than inputStream
        JAXBContext jaxbContext = JAXBContext.newInstance(Metadata.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        metadata = (Metadata) jaxbUnmarshaller.unmarshal(in);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    assertNotNull(metadata);
    Idinfo idinfo = metadata.getIdinfo();
    Spdom spdom = idinfo.getSpdom();
    Bounding bounding = spdom.getBounding();
    double minx = bounding.getWestbc();
    double miny = bounding.getSouthbc();
    double maxx = bounding.getEastbc();
    double maxy = bounding.getNorthbc();
    Bbox result = new Bbox();
    result.setBbox(minx, miny, maxx, maxy);
    System.out.println("Parsed Bbox is: " + result.getBbox());
    Bbox expResult = new Bbox();
    expResult.setBbox("BOX(-77.830618 35.344738, -66.813170 46.642941)");
    assertNotNull(result);
    assertTrue(expResult.getBbox().startsWith("BOX(-77.830618 35."));
    assertTrue(expResult.getBbox().equalsIgnoreCase(result.getBbox()));
}
Also used : Bounding(gov.usgs.cida.coastalhazards.xml.model.Bounding) Spdom(gov.usgs.cida.coastalhazards.xml.model.Spdom) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) Metadata(gov.usgs.cida.coastalhazards.xml.model.Metadata) JAXBContext(javax.xml.bind.JAXBContext) Idinfo(gov.usgs.cida.coastalhazards.xml.model.Idinfo) ByteArrayInputStream(java.io.ByteArrayInputStream) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 4 with Metadata

use of gov.usgs.cida.coastalhazards.xml.model.Metadata in project coastal-hazards by USGS-CIDA.

the class MetadataUtilTest method testGetCrsFromFgdcMetadata.

/**
 * Test of getCrsFromFgdcMetadata method, of class MetadataUtil.
 */
@Test
public void testGetCrsFromFgdcMetadata() throws IOException {
    System.out.println("getCrsFromFgdcMetadata");
    // spref is used to determine hte SRS
    System.out.println("testGetBoundingBoxFromFgdcMetadataAE");
    // get the metadata from the test file as a string using this package to locate it ...
    String packageName = this.getClass().getCanonicalName();
    // PackageName: gov.usgs.cida.coastalhazards.rest.data.util.MetadataUtilTest
    System.out.println("PackageName: " + packageName);
    // this is where the test resource is located - gov.usgs.cida.coastalhazards.rest.data + /ne_AEmeta.xml
    String replaced = packageName.replaceAll("[.]", "/");
    String[] names = replaced.split("/util/MetadataUtilTest");
    String packageNameShort = names[0];
    String testFileFullName = packageNameShort + "/" + CRxml;
    String metadataXml = loadResourceAsString(testFileFullName);
    InputStream in = new ByteArrayInputStream(metadataXml.getBytes("UTF-8"));
    Metadata metadata = null;
    // JAXB will require jaxb-api.jar and jaxb-impl.jar part of java 1.6. Much safer way to interrogate xml and maintain than regex
    try {
        // File file = new File(xmlFile);  // FYI: can also be done via file rather than inputStream
        JAXBContext jaxbContext = JAXBContext.newInstance(Metadata.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        metadata = (Metadata) jaxbUnmarshaller.unmarshal(in);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    assertNotNull(metadata);
    // Spref spref = metadata.getSpref();
    Horizsys horizsys = metadata.getSpref().getHorizsys();
    assertNotNull(horizsys);
    String expEllips = "GRS 1980";
    String expHorizdn = "North American Datum 1983";
    double expDenflat = 298.257222101;
    double expSemiaxis = 6378137.0;
    // part I
    String ellips = horizsys.getGeodetic().getEllips();
    String horizdn = horizsys.getGeodetic().getHorizdn();
    double denflat = horizsys.getGeodetic().getDenflat();
    double semiaxis = horizsys.getGeodetic().getSemiaxis();
    assertTrue(expEllips.equalsIgnoreCase(ellips));
    assertTrue(expHorizdn.equalsIgnoreCase(horizdn));
    assertEquals(expDenflat, denflat, expDenflat - denflat);
    assertEquals(expSemiaxis, semiaxis, expSemiaxis - semiaxis);
    // part II
    String mapprojn = horizsys.getPlanar().getMapproj().getMapprojn();
    double feast = horizsys.getPlanar().getMapproj().getMapprojp().getFeast();
    double fnorth = horizsys.getPlanar().getMapproj().getMapprojp().getFnorth();
    double latprjo = horizsys.getPlanar().getMapproj().getMapprojp().getLatprjo();
    double longcm = horizsys.getPlanar().getMapproj().getMapprojp().getLongcm();
    double stdparll = horizsys.getPlanar().getMapproj().getMapprojp().getStdparll();
    String expMapprojn = "Albers Conical Equal Area";
    double expFeast = 0.0;
    double expFnorth = 0.0;
    double expLatprjo = 23.0;
    double expLongcm = -96.0;
    // the second of the two children
    double expStdparll = 45.5;
    assertTrue(expMapprojn.equalsIgnoreCase(mapprojn));
    assertEquals(expFeast, feast, expFeast - feast);
    assertEquals(expFnorth, fnorth, expFnorth - fnorth);
    assertEquals(expLatprjo, latprjo, expLatprjo - latprjo);
    assertEquals(expLongcm, longcm, expLongcm - longcm);
    assertEquals(expStdparll, stdparll, expStdparll - stdparll);
// CoordinateReferenceSystem expResult = null;
// CoordinateReferenceSystem result = MetadataUtil.getCrsFromFgdcMetadata(metadata);
// assertEquals(expResult, result);
}
Also used : Horizsys(gov.usgs.cida.coastalhazards.xml.model.Horizsys) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) Metadata(gov.usgs.cida.coastalhazards.xml.model.Metadata) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 5 with Metadata

use of gov.usgs.cida.coastalhazards.xml.model.Metadata in project coastal-hazards by USGS-CIDA.

the class MetadataUtil method getBoundingBoxFromFgdcMetadata.

public static Bbox getBoundingBoxFromFgdcMetadata(String inMetadata) throws JAXBException, UnsupportedEncodingException {
    Bbox bbox = new Bbox();
    // parse out the WGS84 bbox from the metadata xml
    Metadata metadata = null;
    // JAXB will require jaxb-api.jar and jaxb-impl.jar part of java 1.6. Much safer way to interrogate xml and maintain than regex
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Metadata.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        metadata = (Metadata) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(inMetadata.getBytes("UTF-8")));
    } catch (JAXBException e) {
        // schema used https: www.fgdc.gov/schemas/metadata/fgdc-std-001-1998-sect01.xsd
        log.error("Unable to parse xml file. Has the schema changed? https://www.fgdc.gov/schemas/metadata/fgdc-std-001-1998-sect01.xsd :" + e.getMessage());
        throw e;
    }
    Idinfo idinfo = metadata.getIdinfo();
    Spdom spdom = idinfo.getSpdom();
    Bounding bounding = spdom.getBounding();
    double minx = bounding.getWestbc();
    double miny = bounding.getSouthbc();
    double maxx = bounding.getEastbc();
    double maxy = bounding.getNorthbc();
    Bbox result = new Bbox();
    result.setBbox(minx, miny, maxx, maxy);
    bbox.setBbox(minx, miny, maxx, maxy);
    return bbox;
}
Also used : Bounding(gov.usgs.cida.coastalhazards.xml.model.Bounding) Spdom(gov.usgs.cida.coastalhazards.xml.model.Spdom) ByteArrayInputStream(java.io.ByteArrayInputStream) Bbox(gov.usgs.cida.coastalhazards.model.Bbox) JAXBException(javax.xml.bind.JAXBException) Metadata(gov.usgs.cida.coastalhazards.xml.model.Metadata) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Idinfo(gov.usgs.cida.coastalhazards.xml.model.Idinfo)

Aggregations

Metadata (gov.usgs.cida.coastalhazards.xml.model.Metadata)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 JAXBContext (javax.xml.bind.JAXBContext)5 JAXBException (javax.xml.bind.JAXBException)5 Unmarshaller (javax.xml.bind.Unmarshaller)5 Bbox (gov.usgs.cida.coastalhazards.model.Bbox)3 Bounding (gov.usgs.cida.coastalhazards.xml.model.Bounding)3 Idinfo (gov.usgs.cida.coastalhazards.xml.model.Idinfo)3 Spdom (gov.usgs.cida.coastalhazards.xml.model.Spdom)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 Horizsys (gov.usgs.cida.coastalhazards.xml.model.Horizsys)2 CRSParameters (gov.usgs.cida.coastalhazards.metadata.CRSParameters)1