MetadataUtil { public static CoordinateReferenceSystem getCrsFromFgdcMetadata(String inMetadata) throws FactoryException, JAXBException, UnsupportedEncodingException{ 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) { 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(); 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); return CRS.parseWKT(buildWkt(crsParms)); }  static Document parseMetadataBody(String postBody); static ParsedMetadata parseMetadataXmlFile(String postBody); static String stripXMLProlog(String xml); static List<String> extractStringsFromCswDoc(Document cswDoc, String path); static String extractCollectionDateFromXml(Document xml, String attr); static Bbox getBoundingBoxFromFgdcMetadata(String inMetadata); static CoordinateReferenceSystem getCrsFromFgdcMetadata(String inMetadata); static List<Publication> getResourcesFromXml(Document doc, String path); static final String[] XML_PROLOG_PATTERNS; }
@Test public void testGetCrsFromFgdcMetadata() throws IOException { System.out.println("getCrsFromFgdcMetadata"); System.out.println("testGetBoundingBoxFromFgdcMetadataAE"); String packageName = this.getClass().getCanonicalName(); System.out.println("PackageName: " + packageName); 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; try { JAXBContext jaxbContext = JAXBContext.newInstance(Metadata.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); metadata = (Metadata) jaxbUnmarshaller.unmarshal(in); } catch (JAXBException e) { e.printStackTrace(); } assertNotNull(metadata); 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; 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); 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; 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); }