Hi guys!
I'm trying generate a shapefile from wkt. The following code generate the
shapefile without exception. When I open in the GIS I can see the
attributes table, but the geometry is not displayed.
What's wrong? Anyone may help?
Thanks!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package geotoolsshape;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.PrecisionModel;
import com.vividsolutions.jts.io.WKTReader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
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.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.referencing.CRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
/**
*
* @author Ronoel Jr.
*/
public class WriteShapefile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
final int SRID = 31982;
CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
final CoordinateReferenceSystem CRS_SRID =
factory.createCoordinateReferenceSystem("EPSG:" + SRID);
final SimpleFeatureType TYPE = createFeatureType(CRS_SRID);
final String WKT = "POLYGON ((664536.142 7053453.9656,
664577.45309999958 7053140.001, 665370.62669999991 7053131.7388,
665254.95550000016 7053569.6367, 664536.142 7053453.9656))";
// Geometry
GeometryFactory geometryFactory;
PrecisionModel precision_model = new
PrecisionModel(PrecisionModel.FIXED);
geometryFactory = new GeometryFactory(precision_model, SRID);
WKTReader reader = new WKTReader(geometryFactory);
Geometry geometria = reader.read(WKT);
geometria.setSRID(SRID);
Polygon pol1 = (Polygon) reader.read(WKT);
// Feature
SimpleFeatureBuilder featureBuilder = new
SimpleFeatureBuilder(TYPE);
featureBuilder.add(pol1);
featureBuilder.add("TESTE");
SimpleFeature feature = featureBuilder.buildFeature("2");
feature.setDefaultGeometry(geometria);
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
features.add(feature);
/*
* Get an output file name and create the new shapefile
*/
File newFile = FileUtils.getFile("novo.shp");
ShapefileDataStore newDataStore = new
ShapefileDataStore(newFile.toURI().toURL());
newDataStore.createSchema(TYPE);
/*
* You can comment out this line if you are using the
createFeatureType method (at end of
* class file) rather than DataUtilities.createType
*/
newDataStore.forceSchemaCRS(CRS_SRID);
/*
* Write the features to the shapefile
*/
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource =
newDataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore)
featureSource;
/*
* SimpleFeatureStore has a method to add features from a
* SimpleFeatureCollection object, so we use the
ListFeatureCollection
* class to wrap our list of features.
*/
SimpleFeatureCollection collection = new
ListFeatureCollection(TYPE, features);
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
}
private static SimpleFeatureType
createFeatureType(CoordinateReferenceSystem crs) throws
NoSuchAuthorityCodeException, FactoryException {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Location");
builder.setNamespaceURI("http://www.visaogeo.com.br/");
builder.setCRS(crs); // <- Coordinate reference system
// add attributes in order
builder.add("Polygon", Polygon.class, crs);
builder.setDefaultGeometry("Polygon");
builder.length(15).add("Name", String.class); // <- 15 chars width
for name field
return builder.buildFeatureType();
}
}
--
*Ronoel Júnior*
*Bacharel em Ciência da Computação*
*Pós-Graduado em Geoprocessamento*
*"Evite impressões desnecessárias. Antes de imprimir pense em sua
responsabilidade e compromisso com o meio ambiente."*
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users