Repository navigation
Add Geometry.getCoordinateDimension #1220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
460d385
1ecfc60
d2f3b1a
26416a3
6b2b3c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /* | ||
| * Copyright (c) 2026 Matthew de Detrich. | ||
| * | ||
| * All rights reserved. This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * and Eclipse Distribution License v. 1.0 which accompanies this distribution. | ||
| * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html | ||
| * and the Eclipse Distribution License is available at | ||
| * | ||
| * http://www.eclipse.org/org/documents/edl-v10.php. | ||
| */ | ||
|
|
||
| package org.locationtech.jts.geom; | ||
|
|
||
| import org.locationtech.jts.geom.impl.CoordinateArraySequenceFactory; | ||
| import org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory; | ||
| import org.locationtech.jts.io.WKTReader; | ||
|
|
||
| import junit.textui.TestRunner; | ||
| import test.jts.GeometryTestCase; | ||
|
|
||
| /** | ||
| * Tests for {@link Geometry#getCoordinateDimension()}. | ||
| */ | ||
| public class GeometryCoordinateDimensionTest extends GeometryTestCase { | ||
| public static void main(String args[]) { | ||
| TestRunner.run(GeometryCoordinateDimensionTest.class); | ||
| } | ||
|
|
||
| static GeometryFactory geomFact = new GeometryFactory(PackedCoordinateSequenceFactory.DOUBLE_FACTORY); | ||
|
|
||
| /** | ||
| * A reader with the legacy "unlabelled 3rd ordinate is Z" syntax disabled, | ||
| * so that WKT lacking a Z or M tag is parsed as genuinely 2D. | ||
| */ | ||
| static WKTReader strictReader = strictReader(); | ||
|
|
||
| private static WKTReader strictReader() { | ||
| WKTReader reader = new WKTReader(geomFact); | ||
| reader.setIsOldJtsCoordinateSyntaxAllowed(false); | ||
| return reader; | ||
| } | ||
|
|
||
| static GeometryFactory arrayGeomFact = new GeometryFactory(CoordinateArraySequenceFactory.instance()); | ||
|
|
||
| static WKTReader arrayStrictReader = arrayStrictReader(); | ||
|
|
||
| private static WKTReader arrayStrictReader() { | ||
| WKTReader reader = new WKTReader(arrayGeomFact); | ||
| reader.setIsOldJtsCoordinateSyntaxAllowed(false); | ||
| return reader; | ||
| } | ||
|
|
||
| public GeometryCoordinateDimensionTest(String name) { super(name); } | ||
|
|
||
| public void testXY() { | ||
| checkDimension("POINT (1 2)", 2); | ||
| } | ||
|
|
||
| public void testXYZ() { | ||
| checkDimension("POINT Z (1 2 3)", 3); | ||
| } | ||
|
|
||
| public void testXYM() { | ||
| checkDimension("POINT M (1 2 3)", 3); | ||
| } | ||
|
|
||
| public void testXYZM() { | ||
| checkDimension("POINT ZM (1 2 3 4)", 4); | ||
| } | ||
|
|
||
| public void testLineStringXYZ() { | ||
| checkDimension("LINESTRING Z (1 1 1, 2 2 2, 3 3 3)", 3); | ||
| } | ||
|
|
||
| public void testPolygonXYZ() { | ||
| checkDimension("POLYGON Z ((1 9 2, 9 9 2, 9 1 2, 1 1 2, 1 9 2))", 3); | ||
| } | ||
|
|
||
| public void testEmptyPoint() { | ||
| checkDimension("POINT EMPTY", 2); | ||
| } | ||
|
Comment on lines
+80
to
+82
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
New test file also needs the license header |
||
|
|
||
| public void testEmptyPointZ() { | ||
| checkDimension("POINT Z EMPTY", 3); | ||
| } | ||
|
|
||
| public void testEmptyPointZM() { | ||
| checkDimension("POINT ZM EMPTY", 4); | ||
| } | ||
|
|
||
| public void testEmptyLineStringZ() { | ||
| checkDimension("LINESTRING Z EMPTY", 3); | ||
| } | ||
|
|
||
| public void testGeometryCollectionEmptyZ() { | ||
| checkDimension("GEOMETRYCOLLECTION (POINT Z EMPTY, LINESTRING EMPTY)", 3); | ||
| } | ||
|
|
||
| public void testMultiPolygonXYZM() { | ||
| checkDimension("MULTIPOLYGON ZM (((1 9 2 3, 9 9 2 3, 9 1 2 3, 1 1 2 3, 1 9 2 3)))", 4); | ||
| } | ||
|
|
||
| public void testGeometryCollectionMixedDimension() { | ||
| checkDimension("GEOMETRYCOLLECTION (POINT (1 1), LINESTRING Z (1 1 1, 2 2 2))", 3); | ||
| } | ||
|
|
||
| public void testGeometryCollectionAllEmpty() { | ||
| checkDimension("GEOMETRYCOLLECTION (POINT EMPTY, LINESTRING EMPTY)", 2); | ||
| } | ||
|
|
||
| public void testArraySequenceXY() { | ||
| checkDimension(arrayStrictReader, "POINT (1 2)", 2); | ||
| } | ||
|
|
||
| public void testArraySequenceEmptyPointZ() { | ||
| checkDimension(arrayStrictReader, "POINT Z EMPTY", 3); | ||
| } | ||
|
|
||
| /** | ||
| * Empty geometries created by the factory use the | ||
| * default coordinate sequence dimension (3). | ||
| */ | ||
| public void testArraySequenceFactoryEmptyPoint() { | ||
| assertEquals(3, arrayGeomFact.createPoint().getCoordinateDimension()); | ||
| } | ||
|
|
||
| private void checkDimension(String wkt, int expectedDimension) { | ||
| checkDimension(strictReader, wkt, expectedDimension); | ||
| } | ||
|
|
||
| private void checkDimension(WKTReader reader, String wkt, int expectedDimension) { | ||
| Geometry geom = read(reader, wkt); | ||
| int actual = geom.getCoordinateDimension(); | ||
| assertEquals(expectedDimension, actual); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
XY / XYZ / XYM / XYZM, mixed collections taking the max, and “3 means Z or M” all match GEOS.
LinearRingfalls throughLineString,Multi*throughGeometryCollection. The strict WKT reader is the correct fixture. That is enough for the original #654 2-vs-3 examples.This getter does not distinguish XYZ from XYM (both 3). That matches GEOS. Callers who need the ordinate set still have to look at
hasZ/hasMon the sequence — fine for #654; do not claim it fully drivesWKBWriter.setOutputOrdinates.Empty
MultiLineString/MultiPolygonfrom WKT already drop the Z flag inWKTReader(createMultiLineString()with no flags). Pre-existing; this method cannot invent a dimension those objects do not store.