⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wktawt.jj

📁 用于GIS(全球地理系统)的分析和处理的代码。
💻 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(WktAwt)

package conversion;

import java.util.StringTokenizer;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;

import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.GeneralPath;

public class WktAwt
{
    static class EndOfFile extends Exception {}
    static class EmptyLine extends Exception {}

    /*-----------------------------------------------------*/
    /*- Create AwtShape from Wkt object(s) ----------------*/
    /*-----------------------------------------------------*/

    public static AwtShape makeAwtShape(InputStream in)
	throws ParseException
    {
        WktAwt parser=new WktAwt(in);
        AwtAggregate geom=new AwtAggregate();

        while (true) {
            try {
                geom.add(parser.parseOneLine());
            } catch (EndOfFile e) {
                break;
            } catch (EmptyLine e) {}
        }
        return geom;
    }

    public static AwtShape makeAwtShape(String wkt)
    throws ParseException
    {
        InputStream in=new ByteArrayInputStream(wkt.getBytes());
        return makeAwtShape(in);
    }

    /*-----------------------------------------------------*/
    /*- Main function for testing -------------------------*/
    /*-----------------------------------------------------*/

    static Ellipse2D makeVisiblePoint(Point2D point)
    {
        return new Ellipse2D.Double(point.getX()-5, point.getY()-5, 10, 10);
    }
}

PARSER_END(WktAwt)

TOKEN:
{
    <CONSTANT: (["-","+"])? (
         <FLOAT>
         |<FLOAT>(["e","E"] (["-","+"])?<INTEGER>)?)
    >
    |
    <#FLOAT:
            <INTEGER>
            |<INTEGER>("."<INTEGER>)?
            |"."<INTEGER>
    >
    | <#INTEGER: (<DIGIT>)+>
    | <#DIGIT: ["0" - "9"]>
}

TOKEN:
{
    <POINT: <CONSTANT>(" ")+<CONSTANT>>
}

TOKEN:
{
    <EOL: "\n">
}

SKIP:
{
    "\t"|" "
}

Point2D point():
{Token xy;}
{
    xy=<POINT>
    {
        StringTokenizer tkz=new StringTokenizer(xy.image);
        String xStr=tkz.nextToken();
        String yStr=tkz.nextToken();
        double x=Double.parseDouble(xStr);
        double y=Double.parseDouble(yStr);
        return new Point2D.Double(x,y);
    }
}

Point2D pointText():
{Point2D p=new Point2D.Double(Double.NaN, Double.NaN);}
{
    ("(" p=point() ")" | "EMPTY") {return p;}
}

GeneralPath linestringText():
{
    GeneralPath lineString=new GeneralPath();
    Point2D p;
}
{
   	("("
    p=point() {lineString.moveTo((float)p.getX(), (float)p.getY());}
    ("," p=point() {lineString.lineTo((float)p.getX(), (float)p.getY());}
    )*
    ")"
    | "EMPTY")
    {return lineString;}
}

AwtShape polygonText():
{
	AwtSurface poly=new AwtSurface();
	GeneralPath lineString;
}
{
    ("("
    lineString=linestringText() {
        lineString.closePath();
        poly=new AwtSurface(lineString);
    }
    ("," lineString=linestringText() {
        lineString.closePath();
        poly.addInterior(lineString);
    }
    )*
    ")"
    | "EMPTY")
    {return poly;}
}

AwtShape multipointText():
{
    GeneralPath multi=new GeneralPath();
    Point2D p;
}
{
    ("("
    p=point() {multi.append(new GeneralPath(makeVisiblePoint(p)), false);}
    ("," p=point() {multi.append(new GeneralPath(makeVisiblePoint(p)), false);}
    )*
    ")"
    | "EMPTY")
    {return new AwtSurface(multi);}
}

AwtShape multilinestringText():
{
    GeneralPath multi=new GeneralPath();
    GeneralPath lineString;
}
{
    ("("
    lineString=linestringText() {multi.append(lineString,false);}
    ("," lineString=linestringText() {multi.append(lineString,false);}
    )*
    ")"
    | "EMPTY")
    {return new AwtOutline(multi);}
}

AwtShape multipolygonText():
{
    AwtAggregate multi=new AwtAggregate();
    AwtShape poly;
}
{
    ("("
    poly=polygonText() {multi.add(poly);}
    ("," poly=polygonText() {multi.add(poly);}
    )*
    ")"
    | "EMPTY")
    {return multi;}
}

AwtShape geometrycollectionText():
{
    AwtAggregate collec=new AwtAggregate();
    AwtShape geom;
}
{
    ("("
    geom=geometryTaggedText() {collec.add(geom);}
    ("," geom=geometryTaggedText() {collec.add(geom);}
    )*
    ")"
    | "EMPTY")
    {return collec;}
}

AwtShape pointTaggedText():
{Point2D p;}
{
    "POINT"
    (p=pointText() {return new AwtSurface(makeVisiblePoint(p));})
}

AwtShape linestringTaggedText():
{
    GeneralPath lineString;
}
{
    "LINESTRING" 
    lineString=linestringText() {return new AwtOutline(lineString);}
}

AwtShape multipointTaggedText():
{AwtShape multi;}
{
    "MULTIPOINT"
    multi=multipointText() {return multi;}
}

AwtShape multilinestringTaggedText():
{AwtShape multi;}
{
    "MULTILINESTRING"
    multi=multilinestringText() {return multi;}
}

AwtShape polygonTaggedText():
{AwtShape poly;}
{
    "POLYGON" 
    poly=polygonText() {return poly;}
}

AwtShape multipolygonTaggedText():
{AwtShape multi;}
{
    "MULTIPOLYGON"
    multi=multipolygonText() {return multi;}
}

AwtShape geometrycollectionTaggedText():
{AwtShape collec;}
{
    "GEOMETRYCOLLECTION"
    collec=geometrycollectionText() {return collec;}
}

AwtShape geometryTaggedText():
{
    AwtShape geom;
}
{
    (geom=pointTaggedText()
    | geom=linestringTaggedText()
    | geom=polygonTaggedText()
    | geom=multipointTaggedText()
    | geom=multilinestringTaggedText()
    | geom=multipolygonTaggedText()
    | geom=geometrycollectionTaggedText())
    {return geom;}
}

AwtShape parseOneLine() throws EmptyLine,EndOfFile:
{
    AwtShape geom;
}
{
    (geom=geometryTaggedText())(<EOF>|<EOL>) {return geom;}
    | <EOL> {throw new EmptyLine(); return null;}
    | <EOF> {throw new EndOfFile(); return null;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -