Package com.vividsolutions.jts.io

Examples of com.vividsolutions.jts.io.WKTReader


public class LineStringSelfIntersections {

  public static void main(String[] args)
      throws Exception
  {
    WKTReader rdr = new WKTReader();

    LineString line1 = (LineString) (rdr.read("LINESTRING (0 0, 10 10, 20 20)"));
    showSelfIntersections(line1);
    LineString line2 = (LineString) (rdr.read("LINESTRING (0 40, 60 40, 60 0, 20 0, 20 60)"));
    showSelfIntersections(line2);

  }


{
  public static void main(String[] args)
      throws Exception
  {
    // read a geometry from a WKT string (using the default geometry factory)
    Geometry g1 = new WKTReader().read("LINESTRING (0 0, 10 10, 20 20)");
    System.out.println("Geometry 1: " + g1);

    // create a geometry by specifying the coordinates directly
    Coordinate[] coordinates = new Coordinate[]{new Coordinate(0, 0),
      new Coordinate(10, 10), new Coordinate(20, 20)};

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Geometry geom = null;
        if (value != null) {
            try {
                WKTReader reader = new WKTReader();
                geom = reader.read(value);
                geom.setSRID(4326);
            } catch (ParseException ex) {
                Logger.getLogger(GeometryConverter.class.getName()).log(Level.SEVERE, null, ex);
            }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Geometry geom = null;
        if (value != null) {
            try {
                WKTReader reader = new WKTReader();
                geom = reader.read(value);
                geom.setSRID(4326);
            } catch (ParseException ex) {
                Logger.getLogger(GeometryConverter.class.getName()).log(Level.SEVERE, null, ex);
            }

                        // try and restore from WKT
                        String wkt = map.getBlackboard().getString(AOI_GEOMETRY);
                        if( wkt != null ){
                            // we got one!
                            GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
                            WKTReader reader = new WKTReader(gf);
                            try {
                                Geometry geometry = reader.read(wkt);
                                blackboard.put(AOI_GEOMETRY, geometry );
                            } catch (ParseException e) {
                                // no can do!
                            }
                        }

   * @throws Exception
   */
    @Test
  public void testDiffOnDonut() throws Exception {
        GeometryFactory geometryFactory = new GeometryFactory();
        WKTReader reader = new WKTReader( geometryFactory );
       
       
        //create feature collection with 2 geometries:
        //1. donut minus hole
        Polygon donut = (Polygon) reader.read("POLYGON ((100 100, 120 100, 120 120, 100 120, 100 100),(112 112, 118 112, 118 118, 112 118, 112 112))");
       
        //2. hole
        Polygon hole = (Polygon) reader.read("POLYGON ((112 112, 118 112, 118 118, 112 118, 112 112))");
       
        //add the two geometries to a FeatureCollection
        SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
        b.setName( "Test" );
        b.add( "location", Polygon.class );
        SimpleFeatureType type = b.buildFeatureType();
        SimpleFeature donutFeature = SimpleFeatureBuilder.build(type, new Object[]{donut}, "fid.1");
        SimpleFeature holeFeature = SimpleFeatureBuilder.build(type, new Object[]{hole}, "fid.2");
        DefaultFeatureCollection fc = new DefaultFeatureCollection();
        fc.add(donutFeature);
        fc.add(holeFeature);
       
        //create iterator for collection
        FeatureIterator<SimpleFeature> iterator = fc.features();
       
        //create List<Geometry> for the simulated "drawn" shape
        Polygon userDrawnPoly = (Polygon) reader.read("POLYGON ((114 90, 130 90, 130 130, 114 130, 114 90))");
        List<Geometry> geoms = new ArrayList<Geometry>();
        geoms.add(userDrawnPoly);
       
        DifferenceFeatureCommand.runDifferenceOp(iterator, geoms);
        assertEquals(1, geoms.size());

        // utility class
    }
   
  public static Geometry read(final String wkt) throws ParseException, IllegalStateException {

    WKTReader reader = new WKTReader();
    Geometry geometry;

    geometry = reader.read(wkt);
   
    if(!geometry.isValid()){
        throw new IllegalStateException("the geometry is not valid: " + geometry.toText() ); //$NON-NLS-1$
    }

    @SuppressWarnings("deprecation")//$NON-NLS-1$
    public Object nativeToJava(TransferData transferData) {
      String string = (String) TextTransfer.getInstance().nativeToJava(
          transferData);

      WKTReader reader = new WKTReader();
      try {
        return reader.read(string);
      } catch (ParseException e) {
        // JONES
      }
      return null;
    }

        @SuppressWarnings("deprecation")
        public Object nativeToJava(TransferData transferData) {
            String string = (String) TextTransfer.getInstance().nativeToJava(
                    transferData);

            WKTReader reader = new WKTReader();
            try {
                Geometry read = reader.read(string);
                SimpleFeatureType ft=DataUtilities.createType("Temp Type", "*geom:"+read.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
               
                return SimpleFeatureBuilder.build( ft, new Object[]{read}, null );
            } catch (Exception e) {
                UiPlugin.log("", e); //$NON-NLS-1$

     *      java.lang.Object)
     */
    public void setPropertyValue( Object idObject, Object value ) {
        ID id = (ID) idObject;
        if (id.id == GEOM) {
            WKTReader reader = new WKTReader();
            try {
                geom = reader.read((String) value);
            } catch (ParseException e) {
                throw (RuntimeException) new RuntimeException( ).initCause( e );
            }
        }
       

TOP

Related Classes of com.vividsolutions.jts.io.WKTReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.