package org.opentrafficsim.demo.ntm.IO; /** *
* Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate$, @version $Revision$, by $Author$, initial version 4 Nov 2014
* @author Alexander Verbraeck
* @author Hans van Lint
* @author Peter Knoppers
* @author Guus Tamminga
* @author Yufei Yuan
*/
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.action.SafeAction;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.util.ProgressListener;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.operation.valid.IsValidOp;
import org.opengis.feature.Feature;
import org.opengis.feature.FeatureVisitor;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opentrafficsim.demo.ntm.Area;
/**
* This example reads data for point locations and associated attributes from a comma separated text (CSV) file and exports them
* as a new shapefile. It illustrates how to build a feature type.
*
* Note: to keep things simple in the code below the input file should not have additional spaces or tabs between fields.
*/
public class WriteToShp
{
static SimpleFeatureSource featureSource = null;
public static void createShape(Map
* This method is an improvement on the code used in the main method above (where we used DataUtilities.createFeatureType)
* because we can set a Coordinate Reference System for the FeatureType and a a maximum field length for the 'name' field
* dddd
*/
private static SimpleFeatureType createFeatureTypeMultiPolygon()
{
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Region");
builder.setCRS(DefaultGeographicCRS.WGS84); // DefaultGeographicCRS.WGS84 <- Coordinate reference system
// add attributes in order
builder.add("the_geom", MultiPolygon.class);
builder.length(15).add("Name", String.class); // <- 15 chars width for name field
builder.add("Number", Integer.class);
// build the type
final SimpleFeatureType REGION = builder.buildFeatureType();
return REGION;
}
/**
* Here is how you can use a SimpleFeatureType builder to create the schema for your shapefile dynamically.
*
* This method is an improvement on the code used in the main method above (where we used DataUtilities.createFeatureType)
* because we can set a Coordinate Reference System for the FeatureType and a a maximum field length for the 'name' field
* dddd
*/
private static SimpleFeatureType createFeatureTypePoint()
{
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Point");
builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference system
// add attributes in order
builder.add("the_geom", Point.class);
builder.length(15).add("Name", String.class); // <- 15 chars width for name field
builder.add("Number", Integer.class);
// build the type
final SimpleFeatureType POINT = builder.buildFeatureType();
return POINT;
}
private static int validateFeatureGeometry(ProgressListener progress) throws Exception
{
final SimpleFeatureCollection featureCollection = featureSource.getFeatures();
// Rather than use an iterator, create a FeatureVisitor to check each fature
class ValidationVisitor implements FeatureVisitor
{
public int numInvalidGeometries = 0;
public void visit(Feature f)
{
SimpleFeature feature = (SimpleFeature) f;
Geometry geom = (Geometry) feature.getDefaultGeometry();
if (geom != null && !geom.isValid())
{
numInvalidGeometries++;
System.out.println("Invalid Geoemtry: " + feature.getID());
}
}
}
ValidationVisitor visitor = new ValidationVisitor();
// Pass visitor and the progress bar to feature collection
featureCollection.accepts(visitor, progress);
return visitor.numInvalidGeometries;
}
public static class ValidateGeometryAction extends SafeAction
{
ValidateGeometryAction()
{
super("Validate geometry");
putValue(Action.SHORT_DESCRIPTION, "Check each geometry");
}
public void action(ActionEvent e) throws Throwable
{
int numInvalid = validateFeatureGeometry(null);
String msg;
if (numInvalid == 0)
{
msg = "All feature geometries are valid";
}
else
{
msg = "Invalid geometries: " + numInvalid;
}
JOptionPane.showMessageDialog(null, msg, "Geometry results", JOptionPane.INFORMATION_MESSAGE);
}
}
}