Skip to content
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

PoC getting Antlr4 to work as WktCrs parser. #122

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/ProjNet/CoordinateSystems/CoordinateSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,37 @@
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//
// ProjNet is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with ProjNet; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

using System;
using System.Collections.Generic;
using System.Text;
using ProjNet.IO.CoordinateSystems;
using ProjNet.IO.Wkt;

namespace ProjNet.CoordinateSystems
{
/// <summary>
/// Builds up complex objects from simpler objects or values.
/// </summary>
/// <remarks>
/// <para>CoordinateSystemFactory allows applications to make coordinate systems that
/// <para>CoordinateSystemFactory allows applications to make coordinate systems that
/// is very flexible, whereas the other factories are easier to use.</para>
/// <para>So this Factory can be used to make 'special' coordinate systems.</para>
/// <para>For example, the EPSG authority has codes for USA state plane coordinate systems
/// using the NAD83 datum, but these coordinate systems always use meters. EPSG does not
/// <para>For example, the EPSG authority has codes for USA state plane coordinate systems
/// using the NAD83 datum, but these coordinate systems always use meters. EPSG does not
/// have codes for NAD83 state plane coordinate systems that use feet units. This factory
/// lets an application create such a hybrid coordinate system.</para>
/// </remarks>
public class CoordinateSystemFactory
public class CoordinateSystemFactory
{
/// <summary>
/// Creates an instance of this class
Expand Down Expand Up @@ -64,6 +65,12 @@ public CoordinateSystem CreateFromWkt(string WKT)
return info as CoordinateSystem;
}

public CoordinateSystem CreateFromWktNew(string WKT)
{
var info = WktToProjBuilder.ParseAndBuild(WKT);
return info as CoordinateSystem;
}


/// <summary>
/// Creates a <see cref="CompoundCoordinateSystem"/> [NOT IMPLEMENTED].
Expand All @@ -83,11 +90,11 @@ public CompoundCoordinateSystem CreateCompoundCoordinateSystem(string name, Coor
/// <summary>
/// Creates a <see cref="FittedCoordinateSystem"/>.
/// </summary>
/// <remarks>The units of the axes in the fitted coordinate system will be
/// <remarks>The units of the axes in the fitted coordinate system will be
/// inferred from the units of the base coordinate system. If the affine map
/// performs a rotation, then any mixed axes must have identical units. For
/// example, a (lat_deg,lon_deg,height_feet) system can be rotated in the
/// (lat,lon) plane, since both affected axes are in degrees. But you
/// example, a (lat_deg,lon_deg,height_feet) system can be rotated in the
/// (lat,lon) plane, since both affected axes are in degrees. But you
/// should not rotate this coordinate system in any other plane.</remarks>
/// <param name="name">Name of coordinate system</param>
/// <param name="baseCoordinateSystem">Base coordinate system</param>
Expand Down Expand Up @@ -122,9 +129,9 @@ public FittedCoordinateSystem CreateFittedCoordinateSystem(string name, Coordina
/// Creates a <see cref="ILocalCoordinateSystem">local coordinate system</see>.
/// </summary>
/// <remarks>
/// The dimension of the local coordinate system is determined by the size of
/// the axis array. All the axes will have the same units. If you want to make
/// a coordinate system with mixed units, then you can make a compound
/// The dimension of the local coordinate system is determined by the size of
/// the axis array. All the axes will have the same units. If you want to make
/// a coordinate system with mixed units, then you can make a compound
/// coordinate system from different local coordinate systems.
/// </remarks>
/// <param name="name">Name of local coordinate system</param>
Expand Down Expand Up @@ -219,9 +226,9 @@ public IProjection CreateProjection(string name, string wktProjectionClass, List
/// Creates <see cref="HorizontalDatum"/> from ellipsoid and Bursa-World parameters.
/// </summary>
/// <remarks>
/// Since this method contains a set of Bursa-Wolf parameters, the created
/// Since this method contains a set of Bursa-Wolf parameters, the created
/// datum will always have a relationship to WGS84. If you wish to create a
/// horizontal datum that has no relationship with WGS84, then you can
/// horizontal datum that has no relationship with WGS84, then you can
/// either specify a <see cref="DatumType">horizontalDatumType</see> of <see cref="DatumType.HD_Other"/>, or create it via WKT.
/// </remarks>
/// <param name="name">Name of ellipsoid</param>
Expand Down Expand Up @@ -294,7 +301,7 @@ public ILocalDatum CreateLocalDatum(string name, DatumType datumType)
/// </summary>
/// <param name="name">Name of datum</param>
/// <param name="datumType">Type of datum</param>
/// <returns>Vertical datum</returns>
/// <returns>Vertical datum</returns>
public VerticalDatum CreateVerticalDatum(string name, DatumType datumType)
{
if (string.IsNullOrWhiteSpace(name))
Expand Down Expand Up @@ -322,7 +329,7 @@ public VerticalCoordinateSystem CreateVerticalCoordinateSystem(string name, Vert
}

/// <summary>
/// Creates a <see cref="CreateGeocentricCoordinateSystem"/> from a <see cref="HorizontalDatum">datum</see>,
/// Creates a <see cref="CreateGeocentricCoordinateSystem"/> from a <see cref="HorizontalDatum">datum</see>,
/// <see cref="LinearUnit">linear unit</see> and <see cref="PrimeMeridian"/>.
/// </summary>
/// <param name="name">Name of geocentric coordinate system</param>
Expand Down
275 changes: 275 additions & 0 deletions src/ProjNet/IO/Wkt/WktCrs.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
/*
[The "BSD licence"] Copyright (c) 2023 Nikolay Fiykov All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above
copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* For parsing propeties file (like GeoTools epsg.properties), use starting rule "propsFile". For parsing single WKT CRS definition, use starting rule "wkt".
*/

// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging

grammar WktCrs;


propsFile
: propRow* EOF
;

propRow
: commentLine
| epsgDefLine
;

commentLine
: COMMENT_LINE
;

epsgDefLine
: epsgCode EQ wkt
;

wkt
: compdcs
| projcs
| geogcs
| vertcs
| geoccs
| localcs
| fittedcs
;

fittedcs
: 'FITTED_CS' LPAR name COMMA paramsmt COMMA projcs (COMMA authority)? RPAR
;

paramsmt
: 'PARAM_MT' LPAR name (COMMA parameter)+ RPAR
;

compdcs
: 'COMPD_CS' LPAR name COMMA (projcs | geogcs) COMMA vertcs COMMA authority RPAR
;

projcs
: 'PROJCS' LPAR name COMMA geogcs COMMA projection COMMA (parameter COMMA)+ unit COMMA ((extension COMMA) | (axis COMMA))* authority RPAR
;

geoccs
: 'GEOCCS' LPAR name COMMA datum COMMA primem COMMA unit COMMA (axis COMMA)+ authority RPAR
;

geogcs
: 'GEOGCS' LPAR name COMMA datum COMMA primem COMMA unit (COMMA axis)* (COMMA authority)? RPAR
;

vertcs
: 'VERT_CS' LPAR name COMMA vertdatum COMMA unit COMMA axis COMMA authority RPAR
;

localcs
: 'LOCAL_CS' LPAR name COMMA localdatum COMMA unit COMMA (axis COMMA)+ authority RPAR
;

datum
: 'DATUM' LPAR name COMMA spheroid ((COMMA towgs84) | (COMMA authority))* RPAR
;

vertdatum
: 'VERT_DATUM' LPAR name COMMA type COMMA authority RPAR
;

localdatum
: 'LOCAL_DATUM' LPAR name COMMA type (COMMA authority)? RPAR
;

spheroid
: 'SPHEROID' LPAR name COMMA semiMajorAxis COMMA inverseFlattening (COMMA authority)? RPAR
;

towgs84
: 'TOWGS84' LPAR dx COMMA dy COMMA dz (COMMA ex COMMA ey COMMA ez (COMMA ppm)?)? RPAR
;

extension
: 'EXTENSION' LPAR name COMMA projtext RPAR
;

authority
: 'AUTHORITY' LPAR authorityName COMMA code RPAR
;

primem
: 'PRIMEM' LPAR name COMMA longitude (COMMA unit)? (COMMA authority)? RPAR
;

unit
: 'UNIT' LPAR name COMMA conversionFactor (COMMA authority)? RPAR
;

axis
: 'AXIS' LPAR name COMMA axisOrient RPAR
;

projection
: 'PROJECTION' LPAR name (COMMA authority)? RPAR
;

parameter
: 'PARAMETER' LPAR name COMMA value RPAR
;

authorityName
: '"EPSG"'
| '"ESRI"'
;

axisOrient
: 'EAST'
| 'WEST'
| 'NORTH'
| 'SOUTH'
| 'NORTH_EAST'
| 'NORTH_WEST'
| 'UP'
| 'DOWN'
| 'OTHER'
| 'GEOCENTRIC_X'
| 'GEOCENTRIC_Y'
| 'GEOCENTRIC_Z'
| name
;

epsgCode
: PKEY
| NUMBER
;

name
: TEXT
;

number
: NUMBER
;

type
: NUMBER
;

semiMajorAxis
: NUMBER
;

inverseFlattening
: NUMBER
;

dx
: NUMBER
;

dy
: NUMBER
;

dz
: NUMBER
;

ex
: NUMBER
;

ey
: NUMBER
;

ez
: NUMBER
;

ppm
: NUMBER
;

projtext
: TEXT
;

code
: TEXT
| NUMBER
;

longitude
: NUMBER
;

conversionFactor
: NUMBER
;

value
: NUMBER
;

NUMBER
: PM? INT ('.' INT)? EXP?
;

TEXT
: '"' ('""' | ~'"')* '"'
;

PKEY
: [A-Z] [0-9A-Z]+
;

COMMENT_LINE
: '#' ~[\r\n]*
;

WS
: [ \r\n\t]+ -> skip
;

COMMA
: ','
;

LPAR
: '['
| '('
;

RPAR
: ']'
| ')'
;

EQ
: '='
;

fragment INT
: [0-9]+
;

fragment EXP
: [Ee] PM? INT
;

fragment PM
: '+'
| '-'
;
Loading
Loading