📄 shapefiledatastoretest.java
字号:
/*
* Geotools2 - OpenSource mapping toolkit
* http://geotools.org
* (C) 2002-2006, Geotools Project Managment Committee (PMC)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
package org.geotools.data.shapefile;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.TestData;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultQuery;
import org.geotools.data.FeatureReader;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.FeatureTypes;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.feature.type.BasicFeatureTypes;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.FeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
/**
*
* @source $URL:
* http://svn.geotools.org/geotools/trunk/gt/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/ShapefileDataStoreTest.java $
* @version $Id: ShapefileDataStoreTest.java 29606 2008-03-12 01:56:09Z jdeolive $
* @author Ian Schneider
*/
public class ShapefileDataStoreTest extends TestCaseSupport {
final static String STATE_POP = "shapes/statepop.shp";
final static String STREAM = "shapes/stream.shp";
final static String DANISH = "shapes/danish_point.shp";
final static String CHINESE = "shapes/chinese_poly.shp";
final static FilterFactory2 ff = CommonFactoryFinder
.getFilterFactory2(null);
public ShapefileDataStoreTest(String testName) throws IOException {
super(testName);
}
protected FeatureCollection<SimpleFeatureType, SimpleFeature> loadFeatures(String resource, Query query)
throws Exception {
assertNotNull(query);
URL url = TestData.url(resource);
ShapefileDataStore s = new ShapefileDataStore(url);
FeatureSource<SimpleFeatureType, SimpleFeature> fs = s.getFeatureSource(s.getTypeNames()[0]);
return fs.getFeatures(query);
}
protected FeatureCollection<SimpleFeatureType, SimpleFeature> loadLocalFeaturesM2() throws IOException {
String target = "jar:file:/C:/Documents and Settings/jgarnett/.m2/repository/org/geotools/gt2-sample-data/2.4-SNAPSHOT/gt2-sample-data-2.4-SNAPSHOT.jar!/org/geotools/test-data/shapes/statepop.shp";
URL url = new URL(target);
ShapefileDataStore s = new ShapefileDataStore(url);
FeatureSource<SimpleFeatureType, SimpleFeature> fs = s.getFeatureSource(s.getTypeNames()[0]);
return fs.getFeatures();
}
protected FeatureCollection<SimpleFeatureType, SimpleFeature> loadFeatures(String resource, Charset charset,
Query q) throws Exception {
if (q == null)
q = new DefaultQuery();
URL url = TestData.url(resource);
ShapefileDataStore s = new ShapefileDataStore(url, false, charset);
FeatureSource<SimpleFeatureType, SimpleFeature> fs = s.getFeatureSource(s.getTypeNames()[0]);
return fs.getFeatures(q);
}
protected FeatureCollection<SimpleFeatureType, SimpleFeature> loadFeatures(ShapefileDataStore s)
throws Exception {
return s.getFeatureSource(s.getTypeNames()[0]).getFeatures();
}
public void testLoad() throws Exception {
loadFeatures(STATE_POP, Query.ALL);
}
public void testLoadDanishChars() throws Exception {
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = loadFeatures(DANISH, Query.ALL);
SimpleFeature first = firstFeature(fc);
// Charl�tte, if you can read it with your OS charset
assertEquals("Charl\u00F8tte", first.getAttribute("TEKST1"));
}
public void testLoadChineseChars() throws Exception {
try {
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = loadFeatures(CHINESE, Charset
.forName("GB18030"), null);
SimpleFeature first = firstFeature(fc);
String s = (String) first.getAttribute("NAME");
assertEquals("\u9ed1\u9f99\u6c5f\u7701", s);
} catch (UnsupportedCharsetException notInstalledInJRE){
// this just means you have not installed
// chinese support into your JRE
// (as such it represents a bad configuration
// rather than a test failure)
// we only wanted to ensure that if you have Chinese support
// available - GeoTools can use it
}
}
public void testNamespace() throws Exception {
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map map = new HashMap();
URI namespace = new URI("http://jesse.com");
map.put(ShapefileDataStoreFactory.NAMESPACEP.key, namespace);
map.put(ShapefileDataStoreFactory.URLP.key, TestData.url(STATE_POP));
ShapefileDataStore store = factory.createDataStore(map);
FeatureType schema = store.getSchema();
assertEquals(namespace.toString(), schema.getName().getNamespaceURI());
}
public void testSchema() throws Exception {
URL url = TestData.url(STATE_POP);
ShapefileDataStore shapeDataStore = new ShapefileDataStore(url);
String typeName = shapeDataStore.getTypeNames()[0];
SimpleFeatureType schema = shapeDataStore.getSchema(typeName);
List<AttributeDescriptor> attributes = schema.getAttributes();
assertEquals("Number of Attributes", 253, attributes.size());
}
public void testSpacesInPath() throws Exception {
URL u = TestData.url(TestCaseSupport.class, "folder with spaces/pointtest.shp");
File f = new File(URLDecoder.decode(u.getFile(), "UTF-8"));
assertTrue(f.exists());
ShapefileDataStore s = new ShapefileDataStore(u);
loadFeatures(s);
}
/**
* Test envelope versus old DataSource
*/
public void testEnvelope() throws Exception {
FeatureCollection<SimpleFeatureType, SimpleFeature> features = loadFeatures(STATE_POP, Query.ALL);
ShapefileDataStore s = new ShapefileDataStore(TestData.url(STATE_POP));
String typeName = s.getTypeNames()[0];
FeatureCollection<SimpleFeatureType, SimpleFeature> all = s.getFeatureSource(typeName).getFeatures();
assertEquals(features.getBounds(), all.getBounds());
}
public void testLoadAndVerify() throws Exception {
FeatureCollection<SimpleFeatureType, SimpleFeature> features = loadFeatures(STATE_POP, Query.ALL);
// FeatureCollection<SimpleFeatureType, SimpleFeature> features = loadFeaturesM2();
int count = features.size();
assertTrue("Have features", count > 0);
// assertEquals("Number of Features loaded",49,features.size()); // FILE
// (correct value)
// assertEquals("Number of Features loaded",3, count); // JAR
SimpleFeature firstFeature = firstFeature(features);
SimpleFeatureType schema = firstFeature.getFeatureType();
assertNotNull(schema.getDefaultGeometry());
assertEquals("Number of Attributes", 253, schema.getAttributeCount());
assertEquals("Value of statename is wrong", "Illinois", firstFeature
.getAttribute("STATE_NAME"));
assertEquals("Value of land area is wrong", 143986.61,
((Double) firstFeature.getAttribute("LAND_KM")).doubleValue(),
0.001);
}
public void testLoadAndCheckParentTypeIsPolygon() throws Exception {
FeatureCollection<SimpleFeatureType, SimpleFeature> features = loadFeatures(STATE_POP, Query.ALL);
SimpleFeatureType schema = firstFeature(features).getFeatureType();
assertTrue(FeatureTypes.isDecendedFrom(schema,
BasicFeatureTypes.POLYGON));
assertTrue(FeatureTypes.isDecendedFrom(schema,
BasicFeatureTypes.POLYGON));
assertTrue(FeatureTypes.isDecendedFrom(schema,
FeatureTypes.DEFAULT_NAMESPACE, "polygonFeature"));
}
public void testCreateSchemaWithEmptyCRS() throws Exception {
File file = new File("test.shp");
URL toURL = file.toURL();
ShapefileDataStore ds = new ShapefileDataStore(toURL);
ds.createSchema(DataUtilities.createType("test", "geom:MultiPolygon"));
assertEquals("test", ds.getSchema().getTypeName());
file.deleteOnExit();
file = new File("test.dbf");
file.deleteOnExit();
file = new File("test.shp");
file.deleteOnExit();
file = new File("test.prj");
if (file.exists())
file.deleteOnExit();
file = new File("test.shx");
if (file.exists()){
file.deleteOnExit();
}
}
public void testCreateSchemaWithCRS() throws Exception {
File file = new File("test.shp");
URL toURL = file.toURL();
ShapefileDataStore ds = new ShapefileDataStore(toURL);
SimpleFeatureType featureType = DataUtilities.createType("test", "geom:MultiPolygon:srid=32615");
CoordinateReferenceSystem crs = featureType.getDefaultGeometry().getCRS();
assertNotNull( crs );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -