📄 shapefiledatastore.java
字号:
if ((propertyNames != null)
&& (propertyNames.length == 1)
&& propertyNames[0].equals(defaultGeomName)
&& (filterAttnames.length == 0 || (filterAttnames.length == 1 && filterAttnames[0]
.equals(defaultGeomName)))) {
try {
SimpleFeatureType newSchema = DataUtilities.createSubType(
schema, propertyNames);
return createFeatureReader(typeName,
getAttributesReader(false), newSchema);
} catch (SchemaException se) {
throw new DataSourceException("Error creating schema", se);
}
}
return super.getFeatureReader(typeName, query);
}
protected org.geotools.data.FIDFeatureReader createFeatureReader(String typeName,
ShapefileAttributeReader reader, SimpleFeatureType readerSchema)
throws SchemaException {
return new org.geotools.data.FIDFeatureReader(reader,
new DefaultFIDReader(typeName), readerSchema);
}
/**
* Returns the attribute reader, allowing for a pure shapefile reader, or a
* combined dbf/shp reader.
*
* @param readDbf -
* if true, the dbf fill will be opened and read
*
*
* @throws IOException
*/
protected ShapefileAttributeReader getAttributesReader(boolean readDbf)
throws IOException {
List<AttributeDescriptor> atts = (schema == null) ? readAttributes()
: schema.getAttributes();
if (!readDbf) {
LOGGER
.fine("The DBF file won't be opened since no attributes will be read from it");
atts = new ArrayList(1);
atts.add(schema.getDefaultGeometry());
return new ShapefileAttributeReader(atts, openShapeReader(), null);
}
return new ShapefileAttributeReader(atts, openShapeReader(),
openDbfReader());
}
/**
* Convenience method for opening a ShapefileReader.
*
* @return A new ShapefileReader.
*
* @throws IOException
* If an error occurs during creation.
*/
protected ShapefileReader openShapeReader() throws IOException {
try {
return new ShapefileReader(shpFiles, true, useMemoryMappedBuffer);
} catch (ShapefileException se) {
throw new DataSourceException("Error creating ShapefileReader", se);
}
}
/**
* Convenience method for opening a DbaseFileReader.
*
* @return A new DbaseFileReader
*
* @throws IOException
* If an error occurs during creation.
*/
protected DbaseFileReader openDbfReader() throws IOException {
if (shpFiles.get(ShpFileType.DBF) == null) {
return null;
}
if (isLocal() && !shpFiles.exists(DBF)) {
return null;
}
try {
return new DbaseFileReader(shpFiles, useMemoryMappedBuffer,
dbfCharset);
} catch (IOException e) {
// could happen if dbf file does not exist
return null;
}
}
/**
* Convenience method for opening an index file.
*
* @return An IndexFile
*
* @throws IOException
*/
protected IndexFile openIndexFile() throws IOException {
if (shpFiles.get(SHX) == null) {
return null;
}
if (isLocal() && !shpFiles.exists(SHX)) {
return null;
}
try {
return new IndexFile(shpFiles, this.useMemoryMappedBuffer);
} catch (IOException e) {
// could happen if shx file does not exist remotely
return null;
}
}
/**
* Convenience method for opening a DbaseFileReader.
*
* @return A new DbaseFileReader
*
* @throws IOException
* If an error occurs during creation.
* @throws FactoryException
* DOCUMENT ME!
*/
protected PrjFileReader openPrjReader() throws IOException,
FactoryException {
if (shpFiles.get(PRJ) == null) {
return null;
}
if (isLocal() && !shpFiles.exists(PRJ)) {
return null;
}
try {
return new PrjFileReader(shpFiles);
} catch (IOException e) {
// could happen if prj file does not exist remotely
return null;
}
}
/**
* Get an array of type names this DataStore holds.<BR/>ShapefileDataStore
* will always return a single name.
*
* @return An array of length one containing the single type held.
*/
public String[] getTypeNames() {
return new String[] { getCurrentTypeName(), };
}
/**
* Create the type name of the single FeatureType this DataStore represents.<BR/>
* For example, if the urls path is file:///home/billy/mytheme.shp, the type
* name will be mytheme.
*
* @return A name based upon the last path component of the url minus the
* extension.
*/
protected String createFeatureTypeName() {
return shpFiles.getTypeName();
}
protected String getCurrentTypeName() {
return (schema == null) ? createFeatureTypeName() : schema
.getTypeName();
}
/**
* A convenience method to check if a type name is correct.
*
* @param requested
* The type name requested.
*
* @throws IOException
* If the type name is not available
*/
protected void typeCheck(String requested) throws IOException {
if (!getCurrentTypeName().equals(requested)) {
throw new IOException("No such type : " + requested);
}
}
/**
* Create a FeatureWriter for the given type name.
*
* @param typeName
* The typeName of the FeatureType to write
* @param transaction
* DOCUMENT ME!
*
* @return A new FeatureWriter.
*
* @throws IOException
* If the typeName is not available or some other error
* occurs.
*/
protected FeatureWriter<SimpleFeatureType, SimpleFeature> createFeatureWriter(String typeName,
Transaction transaction) throws IOException {
typeCheck(typeName);
FeatureReader<SimpleFeatureType, SimpleFeature> featureReader;
ShapefileAttributeReader attReader = getAttributesReader(true);
try {
SimpleFeatureType schema = getSchema();
if (schema == null) {
throw new IOException(
"To create a shapefile, you must first call createSchema()");
}
featureReader = createFeatureReader(typeName, attReader, schema);
} catch (Exception e) {
featureReader = new EmptyFeatureReader<SimpleFeatureType, SimpleFeature>(schema);
}
return new ShapefileFeatureWriter(typeName, shpFiles, attReader,
featureReader);
}
/**
* Obtain the FeatureType of the given name. ShapefileDataStore contains
* only one FeatureType.
*
* @param typeName
* The name of the FeatureType.
*
* @return The FeatureType that this DataStore contains.
*
* @throws IOException
* If a type by the requested name is not present.
*/
public SimpleFeatureType getSchema(String typeName) throws IOException {
typeCheck(typeName);
return getSchema();
}
public SimpleFeatureType getSchema() throws IOException {
if (schema == null) {
List<AttributeDescriptor> types = readAttributes();
SimpleFeatureType parent = null;
Class<?> geomType = types.get(0).getType().getBinding();
if ((geomType == Point.class) || (geomType == MultiPoint.class)) {
parent = BasicFeatureTypes.POINT;
} else if ((geomType == Polygon.class)
|| (geomType == MultiPolygon.class)) {
parent = BasicFeatureTypes.POLYGON;
} else if ((geomType == LineString.class)
|| (geomType == MultiLineString.class)) {
parent = BasicFeatureTypes.LINE;
}
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.addAll(types);
builder.setName(createFeatureTypeName());
if (namespace != null) {
builder.setNamespaceURI(namespace);
} else {
builder.setNamespaceURI(BasicFeatureTypes.DEFAULT_NAMESPACE);
}
builder.setAbstract(false);
if (parent != null) {
builder.setSuperType(parent);
}
schema = builder.buildFeatureType();
}
return schema;
}
/**
* Create the AttributeDescriptor contained within this DataStore.
*
* @return List of new AttributeDescriptor
* @throws IOException
* If AttributeType reading fails
*/
protected List<AttributeDescriptor> readAttributes() throws IOException {
ShapefileReader shp = openShapeReader();
DbaseFileReader dbf = openDbfReader();
CoordinateReferenceSystem crs = null;
PrjFileReader prj = null;
try {
prj = openPrjReader();
if (prj != null) {
crs = prj.getCoodinateSystem();
}
} catch (FactoryException fe) {
crs = null;
}
AttributeTypeBuilder build = new AttributeTypeBuilder();
List<AttributeDescriptor> attributes = new ArrayList<AttributeDescriptor>();
try {
Class<?> geometryClass = JTSUtilities.findBestGeometryClass(shp
.getHeader().getShapeType());
build.setNillable(true);
build.setCRS(crs);
build.setBinding(geometryClass);
GeometryType geometryType = build.buildGeometryType();
attributes.add(build.buildDescriptor(
BasicFeatureTypes.GEOMETRY_ATTRIBUTE_NAME, geometryType));
Set<String> usedNames = new HashSet<String>(); // record names in
// case of
// duplicates
usedNames.add(BasicFeatureTypes.GEOMETRY_ATTRIBUTE_NAME);
// take care of the case where no dbf and query wants all =>
// geometry only
if (dbf != null) {
DbaseFileHeader header = dbf.getHeader();
for (int i = 0, ii = header.getNumFields(); i < ii; i++) {
Class attributeClass = header.getFieldClass(i);
String name = header.getFieldName(i);
if (usedNames.contains(name)) {
String origional = name;
int count = 1;
name = name + count;
while (usedNames.contains(name)) {
count++;
name = origional + count;
}
}
usedNames.add(name);
int length = header.getFieldLength(i);
build.setNillable(true);
build.setLength(length);
build.setBinding(attributeClass);
attributes.add(build.buildDescriptor(name));
}
}
return attributes;
} finally {
try {
if (prj != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -