From: Wladimir S. <bo...@gm...> - 2007-01-29 11:10:21
|
Hi, I am creating an Web application to edit shapefiles online. The shapefiles can be points, lines or polygons. I would like to know as I can create new shapefile of these types. For this I use the following code: ************************************** CODE *************************************************** import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.swing.JFileChooser; import org.geotools.data.DataUtilities; import org.geotools.data.DefaultQuery; import org.geotools.data.FeatureReader; import org.geotools.data.FeatureResults; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureStore; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.filter.*; import org.geotools.factory.FactoryRegistryException; import org.geotools.feature.AttributeTypeFactory; import org.geotools.feature.AttributeType; import org.geotools.feature.FeatureType; import org.geotools.feature.FeatureTypeFactory; import org.geotools.feature.Feature; import org.geotools.feature.IllegalAttributeException; import org.geotools.feature.SchemaException; import org.geotools.graph.util.SimpleFileFilter; import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; import com.vividsolutions.jts.geom.*; public class EscriuSHP { public static void main(String[] args) throws FactoryRegistryException, SchemaException, IOException, ParseException, IllegalAttributeException { String rutaSHP = "c:/datos/"; String nomSHP = "test"; String tipusSHP = "LINE"; String[] nomsCamps = {"ID", "NOMBRE", "CASA"}; String[] valorsCamps = {"1","Felipa","25"}; String[] cX = {"0","2","2","0"}; String[] cY = {"0","0","2","2"}; createSHP(rutaSHP, nomSHP, tipusSHP, nomsCamps); //addFeature(rutaSHP, nomSHP, tipusSHP, nomsCamps, valorsCamps, cX, cY); } private static int createSHP(String rutaSHP, String nomSHP, String tipusSHP, String[] nomsCamps) throws FactoryRegistryException, SchemaException, ParseException, IllegalAttributeException, IOException { String URI = rutaSHP + nomSHP + ".shp"; //feature : type creation AttributeType geom; if (tipusSHP.equalsIgnoreCase("LINE")){ geom = AttributeTypeFactory.newAttributeType("the_geom", LineString.class ); } else if(tipusSHP.equalsIgnoreCase("POINT")){ geom = AttributeTypeFactory.newAttributeType("the_geom", Point.class); } else if(tipusSHP.equalsIgnoreCase("POLYGON")){ geom = AttributeTypeFactory.newAttributeType("the_geom", Polygon.class); } else{ return 0; } AttributeType[] camps = new AttributeType[nomsCamps.length + 1]; camps[0] = geom; for (int i = 1 ; i < camps.length; i++){ camps[i] = AttributeTypeFactory.newAttributeType(nomsCamps[i-1], String.class); } FeatureType ftRoad = FeatureTypeFactory.newFeatureType(camps, nomSHP); // datastore creation URL anURL = (new File(URI)).toURL(); ShapefileDataStore datastore = new ShapefileDataStore(anURL); datastore.createSchema(ftRoad); return 1; } } ************************************** END CODE *************************************************** *This method creates the shapefile correctly. When I open with the Arcview 3.x it shows the correct type of geometry and all the fields. * ** ** *Soon I want to add Features to the created shapefile. * ** *For this I use the following code: * ** ************************************** CODE *************************************************** public static int addFeature(String rutaSHP, String nomSHP, String tipusSHP, String[] nomsCamps, String[] valorsCamps, String[] cX, String[] cY) throws FactoryRegistryException, SchemaException, ParseException, IllegalAttributeException, IOException{ String URI = rutaSHP + nomSHP + ".shp"; AttributeType geom; if (tipusSHP.equalsIgnoreCase("LINE")){ geom = AttributeTypeFactory.newAttributeType("the_geom", LineString.class ); } else if(tipusSHP.equalsIgnoreCase("POINT")){ geom = AttributeTypeFactory.newAttributeType("the_geom", Point.class); } else if(tipusSHP.equalsIgnoreCase("POLYGON")){ geom = AttributeTypeFactory.newAttributeType("the_geom", Polygon.class); } else{ return 0; } AttributeType[] camps = new AttributeType[nomsCamps.length + 1]; camps[0] = geom; for (int i = 1 ; i < camps.length; i++){ camps[i] = AttributeTypeFactory.newAttributeType(nomsCamps[i-1], String.class); } FeatureType ftRoad = FeatureTypeFactory.newFeatureType(camps, "road"); // feature : instance attribute creation WKTReader wktReader = new WKTReader(); Object geometry = null; if (tipusSHP.equalsIgnoreCase("LINE")){ geometry = (LineString) wktReader.read("LINESTRING (10 10, 20 20)"); } else if(tipusSHP.equalsIgnoreCase("POINT")){ geometry = (Point) wktReader.read("POINT (10 10)"); } else if(tipusSHP.equalsIgnoreCase("POLYGON")){ geometry = (Polygon) wktReader.read("POLYGON (10 10, 20 10, 20 20, 10 20, 10 10)"); } else{ return 0; } Object[] valors = new Object[valorsCamps.length + 1]; valors[0] = geometry; for (int i = 1 ; i < valorsCamps.length+1; i++){ System.out.println(valorsCamps[i-1]); valors[i] = valorsCamps[i-1]; } for (int j = 0 ; j < valors.length; j++){ System.out.println("VALOR " + j + " : " + valors[j]); } Feature theRoad = ftRoad.create(valors, "MyRoad"); // getting the feature URL anURL = (new File(URI)).toURL(); ShapefileDataStore datastore = new ShapefileDataStore(anURL); String featureName = datastore.getTypeNames()[0]; // road FeatureSource source = datastore.getFeatureSource(featureName); // appending feature, this will automatically append to the end of shapefile FeatureStore featureStore = (FeatureStore) (datastore.getFeatureSource (featureName)); FeatureReader aReader = DataUtilities.reader(new Feature[] { theRoad }); featureStore.addFeatures(aReader); return 1; } ************************************** END CODE *************************************************** *When attempt to add Features, gives the following error* ******************************** ERROR ************************************************ 29/01/2007 12:06:54 org.geotools.data.shapefile.ShapefileDataStoreopenPrjReader WARNING: projection (.prj) for shapefile: file:/c:/datos/test.shp is not available 29/01/2007 12:06:54 org.geotools.data.shapefile.ShapefileDataStoreopenPrjReader WARNING: projection (.prj) for shapefile: file:/c:/datos/test.shp is not available java.lang.NullPointerException at org.geotools.data.shapefile.ShapefileDataStore$Writer.write( ShapefileDataStore.java:1595) at org.geotools.data.shapefile.ShapefileDataStore$Writer.close( ShapefileDataStore.java:1471) at org.geotools.data.InProcessLockingManager$1.close( InProcessLockingManager.java:343) at org.geotools.data.AbstractFeatureStore.addFeatures( AbstractFeatureStore.java:231) at EscriuSHP.addFeature(EscriuSHP.java:139) at EscriuSHP.main(EscriuSHP.java:44) Exception in thread "main" ************************************ END ERROR *************************************** It would thank for much its aid and if is possible an example of code of like creating a new shapefile and add features. thanks, BOLO |