Hi Jody,
I tried your approach and here is what happened:
- When I call the createSchema, it creates the schema but the crs is null;
- Then I call the forceSchemaCRS and it creates the .prj properly but the
schema is set to null;
- Then when I call the command: featureStore =
(FeatureStore<SimpleFeatureType, SimpleFeature>) shapeFileDataStore
.getFeatureSource(featureType.getTypeName());
it is not able to get the right type, throwing an exception:
java.io.IOException: No such type : MinhaFeature
at
org.geotools.data.shapefile.ShapefileDataStore.typeCheck(ShapefileDataStore.java:545)
at
org.geotools.data.shapefile.ShapefileDataStore.getSchema(ShapefileDataStore.java:599)
at
org.geotools.data.shapefile.ShapefileDataStore.getFeatureSource(ShapefileDataStore.java:946)
at br.pucrio.engduto.model.MyLayer.writeShapeFile(MyLayer.java:141)
Then, I tried to look at the code of ShapeFileDataStore to understand what
is going on. It seems that the problem is here:
protected String getCurrentTypeName() {
return (schema == null) ? createFeatureTypeName() : schema
.getTypeName();
}
Once the forceSchemaCRS sets the schema to null, then this method calls the
createFeatureTypeName which basically gets the filename as the featuretype.
The work around I've made was to create a class in the same package of the
ShapeFileDataStore, as in this way I was able to access the protected schema
attribute, and to set the schema to the previously created schema by the
first createSchema invocation. Now it is generating the .prj properly.
I know it is not an elegant solution, but i was not able to find a better
way. Suggestions are well come.
Here is the code:
package org.geotools.data.shapefile;
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.geotools.feature.FeatureCollection;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
public class MyShapeFileDataStore {
public static ShapefileDataStore
createShapeFileDataStore(FeatureCollection<SimpleFeatureType, SimpleFeature>
collection, CoordinateReferenceSystem crs, File file)throws Exception{
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<String,
Serializable>();
create.put("url", file.toURI().toURL());
create.put("create spatial index", Boolean.TRUE);
ShapefileDataStore shapeFileDataStore = (ShapefileDataStore) factory
.createNewDataStore(create);
shapeFileDataStore.createSchema(collection.getSchema());
SimpleFeatureType schema = shapeFileDataStore.getSchema(); // gets
the created schema
shapeFileDataStore.forceSchemaCRS(crs); // this call is setting the
schema to null
shapeFileDataStore.schema = schema; // it sets the schema again
return shapeFileDataStore;
}
}
2009/2/4 Jody Garnett <[email protected]>
> Hey.
>
> The shapefile datastore does not seem to be writing out the prj very well.
> I know a work around - but I think you are the first person to look at why
> this problem is occurring.
>
> After calling:
>
> shapefileDataStore.createSchema(collection.getSchema());
>
> You can force the prj file to be written:
> shapefile.|*forceSchemaCRS
> <../../../../org/geotools/data/shapefile/ShapefileDataStore.html#forceSchemaCRS%28org.opengis.referencing.crs.CoordinateReferenceSystem%29>*(crs)|
>
> This workaround has been present in the CSV2Shp Lab for a while :
> - http://docs.codehaus.org/display/GEOTDOC/06+CSV2SHP+Lab
>
> Jody
>
> Rodrigo Paes wrote:
>
>>
>> Hello,
>>
>>
>> I'm trying to save a shapefile from a MemoryDataStore.
>>
>> The shapefile seems to be ok, however it is not saving the .prj file
>> properly, when I open it, it is empty (0 bytes).
>>
>>
>> When I call the createSchema(), I'm getting the following warning: "PRJ
>> file not generated for null CoordinateReferenceSystem". Then I tried some
>> debugging and I have found out that this piece of code found in class
>> ShapeFileDataStore.java is throwing an exception:
>>
>> *if* (cs != *null*) {
>>
>> *try* {
>>
>> transformedBounds = env.transform(cs, *true*); <<-
>> exception happens here
>>
>> } *catch* (Exception e) {
>> cs = null;
>> transformedBounds = env;
>> }
>>
>>
>> Please, help.
>>
>>
>>
>> This is the message of the exception that is being caught in the
>> ShapeFileDataStore.java:
>>
>> org.geotools.referencing.operation.projection.ProjectionException: The
>> transform result may be 111.319,871 meters away from the expected position.
>> Are you sure that the input coordinates are inside this map projection area
>> of validity? The point is located 0°00.0'E away from the central meridian
>> and 90°00.0'S away from the latitude of origin. The projection is
>> "Transverse_Mercator"
>>
>>
>>
>> This is a println of the CRS of my GeometryDescriptor:
>>
>>
>> PROJCS["SAD_1969_UTM_Zone_23S",
>>
>> GEOGCS["GCS_South_American_1969",
>>
>> DATUM["D_South_American_1969",
>>
>> SPHEROID["GRS_1967_Truncated", 6378160.0, 298.25]],
>>
>> PRIMEM["Greenwich", 0.0],
>>
>> UNIT["degree", 0.017453292519943295],
>>
>> AXIS["Longitude", EAST],
>>
>> AXIS["Latitude", NORTH]],
>>
>> PROJECTION["Transverse_Mercator"],
>>
>> PARAMETER["central_meridian", -45.0],
>>
>> PARAMETER["latitude_of_origin", 0.0],
>>
>> PARAMETER["scale_factor", 0.9996],
>>
>> PARAMETER["false_easting", 500000.0],
>>
>> PARAMETER["false_northing", 10000000.0],
>>
>> UNIT["m", 1.0],
>>
>> AXIS["x", EAST],
>>
>> AXIS["y", NORTH]]
>>
>>
>>
>> This is the code:
>>
>>
>> FeatureSource<SimpleFeatureType, SimpleFeature> memoryFeatureSource =
>> getFeatureSource();
>>
>> SimpleFeatureType featureType =
>> memoryFeatureSource.getSchema();
>>
>>
>> File file = *new* File(getFileName());
>>
>> Map map = Collections./singletonMap/("url",
>> file.toURI().toURL());
>>
>> FileDataStoreFactorySpi factory = *new*
>> ShapefileDataStoreFactory();
>>
>> ShapefileDataStore shapeFileDataStore = (ShapefileDataStore)
>> factory
>>
>> .createNewDataStore(map);
>>
>>
>> shapeFileDataStore.createSchema(featureType);
>>
>> FeatureStore shapeFileFeatureStore = (FeatureStore) shapeFileDataStore
>>
>>
>> .getFeatureSource(featureType.getName());
>>
>>
>> Transaction t = new DefaultTransaction();
>>
>> try {
>>
>> Map features =
>> memory.getFeatures(featureType.getTypeName());
>>
>> FeatureCollection collection =
>> FeatureCollections.newCollection();
>>
>> collection.addAll(features.values());
>>
>>
>> shapeFileFeatureStore.addFeatures(collection);
>>
>> t.commit(); // write it out
>>
>> } catch (IOException eek) {
>>
>> eek.printStackTrace();
>>
>> try {
>>
>> t.rollback();
>>
>> } catch (IOException doubleEeek) {
>>
>> // rollback failed?
>>
>> }
>>
>> } finally {
>>
>> t.close();
>>
>> }
>>
>> ------------------------------------------------------------------------
>>
>>
>> ------------------------------------------------------------------------------
>> Create and Deploy Rich Internet Apps outside the browser with
>> Adobe(R)AIR(TM)
>> software. With Adobe AIR, Ajax developers can use existing skills and code
>> to
>> build responsive, highly engaging applications that combine the power of
>> local
>> resources and data with the reach of the web. Download the Adobe AIR SDK
>> and
>> Ajax docs to start building applications today-
>> http://p.sf.net/sfu/adobe-com
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Geotools-gt2-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>>
>>
>
>
------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users