📄 wktgeoxygene.jj
字号:
/*
* This file is part of the GeOxygene project source files.
*
* GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
* the development and deployment of geographic (GIS) applications. It is a open source
* contribution of the COGIT laboratory at the Institut G閛graphique National (the French
* National Mapping Agency).
*
* See: http://oxygene-project.sourceforge.net
*
* Copyright (C) 2005 Institut G閛graphique National
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or any later version.
*
* This library 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
* this library (see file LICENSE if present); if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
options {
STATIC=false;
}
PARSER_BEGIN(WktGeOxygene)
package conversion;
import util.GeOxygeneUtil;
import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.ByteArrayInputStream;
import spatial.coordgeom.DirectPosition;
import spatial.coordgeom.GM_LineString;
import spatial.coordgeom.GM_Polygon;
import spatial.geomprim.GM_Ring;
import spatial.geomprim.GM_Curve;
import spatial.geomaggr.GM_MultiPoint;
import spatial.geomaggr.GM_MultiCurve;
import spatial.geomaggr.GM_MultiSurface;
import spatial.geomroot.GM_Object;
import spatial.geomaggr.GM_Aggregate;
import spatial.geomprim.GM_Point;
public class WktGeOxygene
{
static class EndOfFile extends Exception {}
static class EmptyLine extends Exception {}
/*-----------------------------------------------------*/
/*- Create Wkt object(s) from GM_Object ---------------*/
/*-----------------------------------------------------*/
/*- GM_Aggregate --------------------------------------*/
static String geometryCollectionTaggedText(GM_Aggregate aggregate)
{
StringBuffer result=new StringBuffer();
result.append("GEOMETRYCOLLECTION ");
if (GeOxygeneUtil.isEmpty(aggregate)) result.append("EMPTY");
else {
result.append("(");
for (int i=0; i<aggregate.size(); i++) {
if (i!=0)
result.append(", ");
result.append(makeWkt(aggregate.get(i)));
}
result.append(")");
}
return result.toString();
}
/*- GM_MultiPoint -------------------------------------*/
static String multiPointTaggedText(GM_MultiPoint multiPoint)
{
GM_Point point;
StringBuffer result=new StringBuffer();
result.append("MULTIPOINT ");
if (GeOxygeneUtil.isEmpty(multiPoint)) result.append("EMPTY");
else {
result.append("(");
for (int i=0; i<multiPoint.size(); i++) {
point=(GM_Point)multiPoint.get(i);
if (i!=0)
result.append(", ");
result.append(point(point));
}
result.append(")");
}
return result.toString();
}
/*- GM_MultiCurve -------------------------------------*/
static String multiLineStringTaggedText(GM_MultiCurve multiCurve)
{
GM_LineString lineString;
StringBuffer result=new StringBuffer();
result.append("MULTILINESTRING ");
if (GeOxygeneUtil.isEmpty(multiCurve)) result.append("EMPTY");
else {
result.append("(");
for (int i=0; i<multiCurve.size(); i++) {
lineString=(GM_LineString)multiCurve.get(i);
if (i!=0)
result.append(", ");
result.append(lineStringText(lineString));
}
result.append(")");
}
return result.toString();
}
/*- GM_MultiSurface -----------------------------------*/
static String multiPolygon(GM_MultiSurface multiSurface)
{
StringBuffer result=new StringBuffer();
for (int i=0; i<multiSurface.size(); i++) {
GM_Object surface;
surface=multiSurface.get(i);
if (i!=0)
result.append(", ");
if (surface instanceof GM_Polygon)
result.append(polygonText((GM_Polygon)surface));
else if (surface instanceof GM_MultiSurface)
result.append(multiPolygon((GM_MultiSurface)surface));
}
return result.toString();
}
static String multiPolygonText(GM_MultiSurface multiSurface)
{
StringBuffer result=new StringBuffer();
result.append("(");
result.append(multiPolygon(multiSurface));
result.append(")");
return result.toString();
}
static String multiPolygonTaggedText(GM_MultiSurface multiSurface)
{
StringBuffer result=new StringBuffer();
result.append("MULTIPOLYGON ");
if (GeOxygeneUtil.isEmpty(multiSurface)) result.append("EMPTY");
else {
result.append(multiPolygonText(multiSurface));
}
return result.toString();
}
/*- GM_LineString -------------------------------------*/
static String lineStringText(GM_LineString lineString)
{
GM_Point point;
StringBuffer result=new StringBuffer();
result.append("(");
for (int i=0; i<lineString.sizeControlPoint(); i++) {
point=(GM_Point)new GM_Point(lineString.getControlPoint(i));
if (i!=0)
result.append(", ");
result.append(point(point));
}
result.append(")");
return result.toString();
}
static String lineStringTaggedText(GM_LineString lineString)
{
StringBuffer result=new StringBuffer();
result.append("LINESTRING ");
if (GeOxygeneUtil.isEmpty(lineString)) result.append("EMPTY");
else result.append(lineStringText(lineString));
return result.toString();
}
/*- GM_Polygon ----------------------------------------*/
static String polygonText(GM_Polygon polygon)
{
GM_LineString lineString;
GM_Curve prim;
StringBuffer result=new StringBuffer();
result.append("(");
lineString=polygon.exteriorLineString();
result.append(lineStringText(lineString));
for (int i=0; i<polygon.sizeInterior(); i++) {
lineString=polygon.interiorLineString(i);
result.append(", ");
result.append(lineStringText(lineString));
}
result.append(")");
return result.toString();
}
static String polygonTaggedText(GM_Polygon polygon)
{
StringBuffer result=new StringBuffer();
result.append("POLYGON ");
if (GeOxygeneUtil.isEmpty(polygon)) result.append("EMPTY");
else result.append(polygonText(polygon));
return result.toString();
}
/*- GM_Point ------------------------------------------*/
static String point(GM_Point point)
{
DirectPosition position=point.getPosition();
StringBuffer result=new StringBuffer();
result.append(position.getX());
result.append(" ");
result.append(position.getY());
return result.toString();
}
static String pointText(GM_Point point)
{
StringBuffer result=new StringBuffer();
result.append("(");
result.append(point(point));
result.append(")");
return result.toString();
}
static String pointTaggedText(GM_Point point)
{
StringBuffer result=new StringBuffer();
result.append("POINT ");
if (GeOxygeneUtil.isEmpty(point)) result.append("EMPTY");
else result.append(pointText(point));
return result.toString();
}
/*- GM_Object -----------------------------------------*/
public static String makeWkt(GM_Object object)
{
String result="POINT EMPTY";
if (object instanceof GM_Point)
result=pointTaggedText((GM_Point)object);
else if (object instanceof GM_MultiSurface)
result=multiPolygonTaggedText((GM_MultiSurface)object);
else if (object instanceof GM_MultiCurve)
result=multiLineStringTaggedText((GM_MultiCurve)object);
else if (object instanceof GM_MultiPoint)
result=multiPointTaggedText((GM_MultiPoint)object);
else if (object instanceof GM_Polygon)
result=polygonTaggedText((GM_Polygon)object);
else if (object instanceof GM_LineString)
result=lineStringTaggedText((GM_LineString)object);
else if (object instanceof GM_Aggregate)
result=geometryCollectionTaggedText((GM_Aggregate)object);
return result;
}
public static String makeWkt(List geomList)
{
StringBuffer result=new StringBuffer();
Iterator i=geomList.iterator();
while (i.hasNext()) {
GM_Object geom=(GM_Object)i.next();
String wkt=makeWkt(geom);
result.append(wkt);
result.append('\n');
}
return result.toString();
}
/*- Read from stream ----------------------------------*/
public static GM_Object readGeOxygeneFromWkt(BufferedReader in)
throws IOException,ParseException
{
String wkt=in.readLine();
return makeGeOxygene(wkt);
}
public static GM_Object readGeOxygeneFromWkt(InputStream in)
throws IOException,ParseException
{
return readGeOxygeneFromWkt(new BufferedReader(new InputStreamReader(in)));
}
public static GM_Object readGeOxygeneFromWkt(String path)
throws FileNotFoundException,IOException,ParseException
{
return readGeOxygeneFromWkt(new FileInputStream(path));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -