@Test public void testCRSConverter() throws Exception { CoordinateReferenceSystem crs = CRS.decode("EPSG:4326"); CRSConverter c = new CRSConverter(); assertEquals(crs.toWKT(), c.toString(crs)); assertEquals(DefaultGeographicCRS.WGS84.toWKT(), c.toString(DefaultGeographicCRS.WGS84)); CoordinateReferenceSystem crs2 = (CoordinateReferenceSystem) c.fromString(crs.toWKT()); assertTrue(CRS.equalsIgnoreMetadata(crs, crs2)); crs2 = (CoordinateReferenceSystem) c.fromString("EPSG:4326"); assertTrue(CRS.equalsIgnoreMetadata(crs, crs2)); }
/** * This method is responsible for creating a projection file using the WKT representation of * this coverage's coordinate reference system. We can reuse this file in order to rebuild later * the crs. * * @param baseFile * @param coordinateReferenceSystem * @throws IOException */ private static void createProjectionFile( final String baseFile, final CoordinateReferenceSystem coordinateReferenceSystem) throws IOException { final File prjFile = new File(new StringBuffer(baseFile).append(".prj").toString()); BufferedWriter out = new BufferedWriter(new FileWriter(prjFile)); out.write(coordinateReferenceSystem.toWKT()); out.close(); }
/** * Look for a dataset global {@link CoordinateReferenceSystem} definition provided through a * spatial_ref global attribute. * * @param dataset * @return */ public static CoordinateReferenceSystem lookForDatasetCRS(NetcdfDataset dataset) { CoordinateReferenceSystem projection = NetCDFProjection.parseProjection(dataset); if (projection != null) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine( "Detected NetCDFProjection through spatial_ref attribute: " + projection.toWKT()); } } return projection; } }
/** * Turns the CRS into a single line WKT, more compatible with ESRI software * * @param crs * @return */ String toSingleLineWKT(CoordinateReferenceSystem crs) { String wkt = null; try { // this is a lenient transformation, works with polar stereographics too Formattable formattable = (Formattable) crs; wkt = formattable.toWKT(0, false); } catch (ClassCastException e) { wkt = crs.toWKT(); } wkt = wkt.replaceAll("\n", "").replaceAll(" ", ""); return wkt; }
/** * Adds a crs to the geopackage, registering it in the spatial_ref_sys table. * * @param crs The crs to add. * @param auth The authority code, example: epsg * @param srid The spatial reference system id. */ public void addCRS(CoordinateReferenceSystem crs, String auth, int srid) throws IOException { Connection cx; try { cx = connPool.getConnection(); try { GeoPackage.addCRS( cx, srid, auth + ":" + srid, auth, srid, crs.toWKT(), auth + ":" + srid); } finally { cx.close(); } } catch (SQLException e) { throw new RuntimeException(e); } }
tmp.put(YRES, String.valueOf(yRes)); tmp.put(NO_DATA, String.valueOf(noData)); tmp.put(CRS, crs.toWKT());
/** * Takes care of writing the world file for this geotiff * * @param gc the {@link GridCoverage} to take the georefeerincing from. * @param tr * @throws IOException in case something bad occurs while writing. */ private void handleTFW(final GridCoverage gc, AffineTransform tr) throws IOException { final File destFile = (File) this.destination; final File parentFile = destFile.getParentFile(); final String name = destFile.getName(); final File tfw = new File(parentFile, name.replace("tif", "tfw")); final File prj = new File(parentFile, name.replace("tif", "prj")); final BufferedWriter outW = new BufferedWriter(new FileWriter(prj)); new WorldFileWriter(tfw, tr); // side effect of creation writes the file try { outW.write(gc.getCoordinateReferenceSystem().toWKT()); } finally { try { outW.close(); } catch (Exception e) { LOGGER.log(Level.FINER, "Failed to close output writer", e); } } }
try { fileWriter.write(crs.toWKT()); } finally { try {
/** * Store the {@link FeatureTypeMapper} instance * * @param mapper */ protected void storeMapper(FeatureTypeMapper mapper) { final Properties properties = new Properties(); final String typeName = mapper.getName().toString(); properties.setProperty(NAME, typeName); properties.setProperty(MAPPEDNAME, mapper.getMappedName().toString()); final List<Definition> definitions = mapper.getDefinitions(); final StringBuilder builder = new StringBuilder(); // Populating schema for (Definition definition : definitions) { builder.append(definition.getName()) .append(":") .append(definition.getBinding().getName()) .append(","); } String schema = builder.toString(); schema = schema.substring(0, schema.length() - 1); properties.setProperty(SCHEMA, schema); properties.setProperty( COORDINATE_REFERENCE_SYSTEM, mapper.getCoordinateReferenceSystem().toWKT()); // Storing properties storeProperties(properties, typeName); }
@Override protected void storeMapper(FeatureTypeMapper mapper) { final Properties properties = new Properties(); final String typeName = mapper.getName().toString(); properties.setProperty(NAME, typeName); properties.setProperty(MAPPEDNAME, mapper.getMappedName().toString()); final List<Definition> definitions = mapper.getDefinitions(); final StringBuilder builder = new StringBuilder(); // Populating schema for (Definition definition : definitions) { builder.append(definition.getName()) .append(":") .append(definition.getBinding().getName()) .append(","); } String schema = builder.toString(); schema = schema.substring(0, schema.length() - 1); properties.setProperty(SCHEMA, schema); properties.setProperty( COORDINATE_REFERENCE_SYSTEM, mapper.getCoordinateReferenceSystem().toWKT()); properties.setProperty( SRID, Integer.toString(((PostgisFeatureTypeMapper) mapper).getSrID())); // Storing properties storeProperties(properties, typeName); } }
/** * Look for a CoordinateReferenceSystem defined into a variable * * @param dataset * @param defaultCrs * @return * @throws FactoryException */ public static CoordinateReferenceSystem lookForVariableCRS( NetcdfDataset dataset, CoordinateReferenceSystem defaultCrs) throws FactoryException { List<Variable> variables = dataset.getVariables(); CoordinateReferenceSystem crs = defaultCrs; for (Variable variable : variables) { CRSParser attrib = CRSParser.getCRSParser(variable); if (attrib != null) { // Referencing info found crs = NetCDFProjection.parseProjection(variable, attrib); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine( "Detected NetCDFProjection through gridMapping variable: " + (crs != null ? crs.toWKT() : "null")); } break; } } return crs; }
sb.append(", levels: ").append(getNumLevels()); sb.append(", tile size: ").append(getTileWidth()).append("x").append(getTileHeight()); sb.append(", crs: ").append(srs == null ? getCoordinateReferenceSystem().toWKT() : srs); GeneralEnvelope env = getOriginalEnvelope(); sb.append(", Envelope: ")
/** Displays the CRS definition in a dialog. */ private void showCRS() { if (mapPane != null && mapPane.getMapContent() != null) { CoordinateReferenceSystem crs = mapPane.getMapContent().getCoordinateReferenceSystem(); JTextReporter.showDialog( "Coordinate reference system", crs.toWKT(), JTextReporter.FLAG_MODAL); } } }
node.setAttribute(EAST, Double.toString(east)); node.setAttribute(WEST, Double.toString(west)); node.setAttribute(CRS, crs.toWKT()); root.appendChild(node);
if (tempCRS != null) { this.crs = (CoordinateReferenceSystem) tempCRS; LOGGER.log(Level.WARNING, "Using forced coordinate reference system " + crs.toWKT()); } else { final CoordinateReferenceSystem tempcrs = crsReader.getCoordinateReferenceSystem(); Level.WARNING, "Unable to find a CRS for this coverage, using a default one: " + crs.toWKT()); } else crs = tempcrs;
} else { LOGGER.info("Creating the SeCoordRef object for CRS " + crs); WKT = crs.toWKT(); coordref.setCoordSysByDescription(WKT);
new IOException( "Unable to find nort dimension in the coverage CRS+ " + coverageCRS.toWKT()); throw ioe;
/** Test that parameter values are correctly converted to WKT. */ @Test public void toWKT() throws Exception { // @formatter:off CoordinateReferenceSystem crs = CRS.parseWKT( "PROJCS[\"unnamed\", " + "GEOGCS[\"unnamed ellipse\", " + "DATUM[\"unknown\", SPHEROID[\"unnamed\",6370841.391468334,0]], " + "PRIMEM[\"Greenwich\",0], " + "UNIT[\"degree\",0.0174532925199433]], " + "PROJECTION[\"Azimuthal_Equidistant\"], " + "PARAMETER[\"latitude_of_center\",42.42], " + "PARAMETER[\"longitude_of_center\",16.16], " + "PARAMETER[\"false_easting\",100000], " + "PARAMETER[\"false_northing\",200000]," + "UNIT[\"metre\", 1, AUTHORITY[\"EPSG\",\"9001\"]]]"); // @formatter:on String wkt = crs.toWKT(); Assert.assertTrue(wkt.contains("PROJECTION[\"Azimuthal_Equidistant\"]")); Assert.assertTrue(wkt.contains("PARAMETER[\"latitude_of_center\", 42.42]")); Assert.assertTrue(wkt.contains("PARAMETER[\"longitude_of_center\", 16.16]")); Assert.assertTrue(wkt.contains("PARAMETER[\"false_easting\", 100000.0]")); Assert.assertTrue(wkt.contains("PARAMETER[\"false_northing\", 200000.0]")); } }
@Test public void testLambertParsing() throws FactoryException { String initialLambertWkt = "PROJCS[\"LAMBERT WKT\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\"," + "SPHEROID[\"WGS_1984\",6371200.0,0]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\"," + "0.017453292519943295]],PROJECTION[\"Lambert_Conformal_Conic\"]," + "PARAMETER[\"standard_parallel_1\",25.0],PARAMETER[\"latitude_of_origin\",25.0]," + "PARAMETER[\"central_meridian\",-95.0],PARAMETER[\"false_easting\",0]," + "PARAMETER[\"false_northing\",0],PARAMETER[\"Scale_Factor\",1.0],UNIT[\"m\",1]]"; CoordinateReferenceSystem lambertCRS = CRS.parseWKT(initialLambertWkt); String parsedLambertWkt = lambertCRS.toWKT(); CoordinateReferenceSystem lambertCRS2 = CRS.parseWKT(parsedLambertWkt); assertTrue(CRS.equalsIgnoreMetadata(lambertCRS, lambertCRS2)); }
/** * Tests parsing with custom axis length. At the difference of the previous test, the WKT * formatting in this test should include the axis length as parameter values. */ @Test public void testCustomAxisLength() throws FactoryException, ParseException { DefaultMathTransformFactory factory = new DefaultMathTransformFactory(); ParameterValueGroup parameters = factory.getDefaultParameters("Lambert_Conformal_Conic_2SP"); final double majorAxis = 6.3712e+6; final double minorAxis = 6.3712e+6; parameters.parameter("semi_major").setValue(majorAxis); parameters.parameter("semi_minor").setValue(minorAxis); parameters.parameter("latitude_of_origin").setValue(25.0); parameters.parameter("standard_parallel_1").setValue(25.0); parameters.parameter("standard_parallel_2").setValue(25.0); parameters.parameter("longitude_of_origin").setValue(-95.0); parameters.parameter("false_easting").setValue(0.0); parameters.parameter("false_northing").setValue(0.0); GeographicCRS base = DefaultGeographicCRS.WGS84; MathTransform mt = factory.createParameterizedTransform(parameters); CartesianCS cs = DefaultCartesianCS.PROJECTED; CoordinateReferenceSystem crs = new DefaultProjectedCRS("Lambert", base, mt, cs); final String wkt = crs.toWKT(); assertTrue(wkt.contains("semi_major")); assertTrue(wkt.contains("semi_minor")); final Parser parser = new Parser(); CoordinateReferenceSystem check = parser.parseCoordinateReferenceSystem(wkt); assertEquals(wkt, check.toWKT()); }