use of gov.usgs.cida.coastalhazards.xml.model.Spdom 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()));
}
use of gov.usgs.cida.coastalhazards.xml.model.Spdom 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()));
}
use of gov.usgs.cida.coastalhazards.xml.model.Spdom 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;
}
Aggregations