Java Code Examples for org.geotools.referencing.CRS#parseWKT()

The following examples show how to use org.geotools.referencing.CRS#parseWKT() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GamaShapeFile.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public ShapeInfo(final String propertiesString) {
	super(propertiesString);
	final String[] segments = split(propertiesString);
	itemNumber = Integer.parseInt(segments[1]);
	final String crsString = segments[2];
	CoordinateReferenceSystem theCRS;
	if ("null".equals(crsString) || crsString.startsWith("Unknown")) {
		theCRS = null;
	} else {
		try {
			theCRS = CRS.parseWKT(crsString);
		} catch (final Exception e) {
			theCRS = null;
		}
	}
	crs = theCRS;
	width = Double.parseDouble(segments[3]);
	height = Double.parseDouble(segments[4]);
	if (segments.length > 5) {
		final String[] names = splitByWholeSeparatorPreserveAllTokens(segments[5], SUB_DELIMITER);
		final String[] types = splitByWholeSeparatorPreserveAllTokens(segments[6], SUB_DELIMITER);
		for (int i = 0; i < names.length; i++) {
			attributes.put(names[i], types[i]);
		}
	}
}
 
Example 2
Source File: GamaShapeFile.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public String toPropertyString() {
	// See Issue #1603: .toWKT() && pa can sometimes cause problem with
	// certain projections.
	String system = crs == null ? "Unknown projection" : crs.toWKT();
	try {
		CRS.parseWKT(system);
	} catch (final Exception e) {
		// The toWKT()/parseWKT() pair has a problem
		String srs = CRS.toSRS(crs);
		if (srs == null && crs != null) {
			srs = crs.getName().getCode();
		}
		system = "Unknown projection " + srs;

	}
	final String attributeNames = join(attributes.keySet(), SUB_DELIMITER);
	final String types = join(attributes.values(), SUB_DELIMITER);
	final Object[] toSave =
			new Object[] { super.toPropertyString(), itemNumber, system, width, height, attributeNames, types };
	return join(toSave, DELIMITER);
}
 
Example 3
Source File: CrsUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reads a {@link CoordinateReferenceSystem} from a prj file.
 *
 * @param filePath  the path to the prj file or the connected datafile it sidecar file for.
 * @param extension the extension of the data file. If <code>null</code> it is assumed to be prj.
 *
 * @return the read {@link CoordinateReferenceSystem}.
 *
 * @throws Exception
 */
@SuppressWarnings("nls")
public static CoordinateReferenceSystem readProjectionFile( String filePath, String extension ) throws Exception {
    CoordinateReferenceSystem crs = null;
    String prjPath = null;
    String filePathLower = filePath.trim().toLowerCase();
    if (filePathLower.endsWith(".prj")) {
        // it is the prj file
        prjPath = filePath;
    } else if (extension != null && filePathLower.endsWith("." + extension)) {
        // datafile was supplied (substitute extension)
        int dotLoc = filePath.lastIndexOf(".");
        prjPath = filePath.substring(0, dotLoc);
        prjPath = prjPath + ".prj";
    } else {
        prjPath = filePath + ".prj";
    }

    File prjFile = new File(prjPath);
    if (!prjFile.exists()) {
        throw new ModelsIOException("The prj file doesn't exist: " + prjPath, "CRSUTILITIES");
    }
    String wkt = FileUtilities.readFile(prjFile);
    crs = CRS.parseWKT(wkt);
    return crs;
}
 
Example 4
Source File: GamaOsmFile.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public OSMInfo(final String propertiesString) throws NoSuchAuthorityCodeException, FactoryException {
	super(propertiesString);
	if (!hasFailed) {
		final String[] segments = split(propertiesString);
		itemNumber = Integer.parseInt(segments[1]);
		final String crsString = segments[2];
		if ("null".equals(crsString)) {
			crs = null;
		} else {
			crs = CRS.parseWKT(crsString);
		}
		width = Double.parseDouble(segments[3]);
		height = Double.parseDouble(segments[4]);
		if (segments.length > 5) {
			final String[] names = splitByWholeSeparatorPreserveAllTokens(segments[5], SUB_DELIMITER);
			final String[] types = splitByWholeSeparatorPreserveAllTokens(segments[6], SUB_DELIMITER);
			for (int i = 0; i < names.length; i++) {
				attributes.put(names[i], types[i]);
			}
		}
	} else {
		itemNumber = 0;
		width = 0.0;
		height = 0.0;
		crs = null;
	}
}
 
Example 5
Source File: MosaicFormModel.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
CoordinateReferenceSystem getTargetCRS() throws FactoryException {
    final String crs = (String) getPropertyValue("crs");
    if (crs == null) {
        return null;
    }
    try {
        return CRS.parseWKT(crs);
    } catch (FactoryException ignored) {
        return CRS.decode(crs, true);
    }
}
 
Example 6
Source File: FeatureLayerType.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object convertDomToValue(DomElement parentElement, Object value) throws ConversionException,
        ValidationException {
    try {
        value = CRS.parseWKT(parentElement.getValue());
    } catch (FactoryException e) {
        throw new IllegalArgumentException(e);
    }
    return value;
}
 
Example 7
Source File: GeoServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Finish service initialization.
 *
 * @throws GeomajasException oops
 */
@PostConstruct
protected void postConstruct() throws GeomajasException {
	if (null != crsDefinitions) {
		for (CrsInfo crsInfo : crsDefinitions.values()) {
			try {
				CoordinateReferenceSystem crs = CRS.parseWKT(crsInfo.getCrsWkt());
				String code = crsInfo.getKey();
				crsCache.put(code, CrsFactory.getCrs(code, crs));
			} catch (FactoryException e) {
				throw new GeomajasException(e, ExceptionCode.CRS_DECODE_FAILURE_FOR_MAP, crsInfo.getKey());
			}
		}
	}
	if (null != crsTransformDefinitions) {
		for (CrsTransformInfo crsTransformInfo : crsTransformDefinitions.values()) {
			String key = getTransformKey(crsTransformInfo);
			transformCache.put(key, getCrsTransform(key, crsTransformInfo));
		}
	}
	GeometryFactory factory = new GeometryFactory();
	EMPTY_GEOMETRIES.put(Point.class, factory.createPoint((Coordinate) null));
	EMPTY_GEOMETRIES.put(LineString.class, factory.createLineString((Coordinate[]) null));
	EMPTY_GEOMETRIES.put(Polygon.class, factory.createPolygon(null, null));
	EMPTY_GEOMETRIES.put(MultiPoint.class, factory.createMultiPoint((Coordinate[]) null));
	EMPTY_GEOMETRIES.put(MultiLineString.class, factory.createMultiLineString((LineString[]) null)); // cast needed!
	EMPTY_GEOMETRIES.put(MultiPolygon.class, factory.createMultiPolygon((Polygon[]) null)); // cast needed!
	EMPTY_GEOMETRIES.put(Geometry.class, factory.createGeometryCollection(null));
}