public Collection<SimpleFeatureType> parseFeatureTypes(String typeName, File file) throws IOException { InputStream inputStream = null; try { inputStream = new FileInputStream(file); return parseFeatureTypes(typeName, inputStream); } finally { if (inputStream != null) { inputStream.close(); } } }
SimpleFeature feature = (SimpleFeature) object; SimpleFeatureType ft = feature.getFeatureType(); aggregateFeatureType = unionFeatureTypes(aggregateFeatureType, ft); Map<Object, Object> userData = feature.getUserData(); @SuppressWarnings("unchecked") featureType = unionFeatureTypes(featureType, schema); featureType = convertParsedFeatureType(featureType, typeName, untypedAttributes); if (!schemaNames.isEmpty()) { Map<Object, Object> userData = featureType.getUserData();
@Override public boolean canRead(ImportData data) throws IOException { File file = getFileFromData(data); return file.canRead() && "kml".equalsIgnoreCase(FilenameUtils.getExtension(file.getName())); }
public void testReadFeatureWithNameAndDescription() throws Exception { String kmlInput = DOC_EL + "<Placemark><name>foo</name><description>bar</description></Placemark></kml>"; SimpleFeatureType featureType = kmlFileFormat.parseFeatureTypes("foo", IOUtils.toInputStream(kmlInput)).get(0); FeatureReader<SimpleFeatureType, SimpleFeature> reader = kmlFileFormat.read(featureType, IOUtils.toInputStream(kmlInput)); assertTrue("No features found", reader.hasNext()); SimpleFeature feature = reader.next(); assertNotNull("Expecting feature", feature); assertEquals("Invalid name attribute", "foo", feature.getAttribute("name")); assertEquals("Invalid description attribute", "bar", feature.getAttribute("description")); }
@Override public List<ImportTask> list(ImportData data, Catalog catalog, ProgressMonitor monitor) throws IOException { File file = getFileFromData(data); CatalogBuilder cb = new CatalogBuilder(catalog); String baseName = typeNameFromFile(file); CatalogFactory factory = catalog.getFactory(); Collection<SimpleFeatureType> featureTypes = parseFeatureTypes(baseName, file); List<ImportTask> result = new ArrayList<ImportTask>(featureTypes.size()); for (SimpleFeatureType featureType : featureTypes) {
@SuppressWarnings("rawtypes") @Override public FeatureReader read(ImportData data, ImportTask task) throws IOException { File file = getFileFromData(data); // we need to get the feature type, to use for the particular parse through the file // since we put it on the metadata from the list method, we first check if that's still // available SimpleFeatureType ft = (SimpleFeatureType) task.getMetadata().get(FeatureType.class); if (ft == null) { // if the type is not available, we can generate one from the resource // we aren't able to ask for the feature type from the resource directly, // because we don't have a backing store FeatureTypeInfo fti = (FeatureTypeInfo) task.getLayer().getResource(); ft = buildFeatureTypeFromInfo(fti); MetadataMap metadata = fti.getMetadata(); if (metadata.containsKey("importschemanames")) { Map<Object, Object> userData = ft.getUserData(); userData.put("schemanames", metadata.get("importschemanames")); } } return read(ft, file); }
@SuppressWarnings("rawtypes") @Override public FeatureReader read(ImportData data, ImportTask task) throws IOException { File file = getFileFromData(data); // we need to get the feature type, to use for the particular parse through the file // since we put it on the metadata from the list method, we first check if that's still available SimpleFeatureType ft = (SimpleFeatureType) task.getMetadata().get(FeatureType.class); if (ft == null) { // if the type is not available, we can generate one from the resource // we aren't able to ask for the feature type from the resource directly, // because we don't have a backing store FeatureTypeInfo fti = (FeatureTypeInfo) task.getLayer().getResource(); SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder(); ftb.setName(fti.getName()); List<AttributeTypeInfo> attributes = fti.getAttributes(); for (AttributeTypeInfo attr : attributes) { ftb.add(attr.getName(), attr.getBinding()); } ft = ftb.buildFeatureType(); MetadataMap metadata = fti.getMetadata(); if (metadata.containsKey("importschemanames")) { Map<Object, Object> userData = ft.getUserData(); userData.put("schemanames", metadata.get("importschemanames")); } } return read(ft, file); }
@Override protected void setUp() throws Exception { kmlFileFormat = new KMLFileFormat(); }
public void testReadCustomSchema() throws Exception { String kmlInput = DOC_EL + "<Schema name=\"myschema\">" + "<SimpleField type=\"int\" name=\"foo\"></SimpleField>" + "</Schema>" + "<myschema><foo>7</foo></myschema>" + "</kml>"; List<SimpleFeatureType> featureTypes = kmlFileFormat.parseFeatureTypes("custom-schema", IOUtils.toInputStream(kmlInput)); assertEquals("Unexpected number of feature types", 1, featureTypes.size()); SimpleFeatureType featureType = featureTypes.get(0); Map<Object, Object> userData = featureType.getUserData(); List<String> schemaNames = (List<String>) userData.get("schemanames"); assertEquals(1, schemaNames.size()); assertEquals("Did not find expected schema name metadata", "myschema", schemaNames.get(0)); FeatureReader<SimpleFeatureType, SimpleFeature> reader = kmlFileFormat.read(featureType, IOUtils.toInputStream(kmlInput)); SimpleFeature feature = reader.next(); assertNotNull("Expecting feature", feature); assertEquals("Invalid ext attr foo", 7, feature.getAttribute("foo")); } }
@Override public List<ImportTask> list(ImportData data, Catalog catalog, ProgressMonitor monitor) throws IOException { File file = getFileFromData(data); CatalogBuilder cb = new CatalogBuilder(catalog); String baseName = typeNameFromFile(file); CatalogFactory factory = catalog.getFactory(); Collection<SimpleFeatureType> featureTypes = parseFeatureTypes(baseName, file); List<ImportTask> result = new ArrayList<ImportTask>(featureTypes.size()); for (SimpleFeatureType featureType : featureTypes) {
public void testReadFeatureWithTypedExtendedData() throws Exception { String kmlInput = DOC_EL + "<Schema name=\"myschema\">" + "<SimpleField type=\"int\" name=\"foo\"></SimpleField>" + "</Schema>" + "<Placemark>" + "<ExtendedData>" + "<SchemaData schemaUrl=\"#myschema\">" + "<SimpleData name=\"foo\">42</SimpleData>" + "</SchemaData>" + "</ExtendedData>" + "</Placemark></kml>"; SimpleFeatureType featureType = kmlFileFormat.parseFeatureTypes("foo", IOUtils.toInputStream(kmlInput)).get(0); FeatureReader<SimpleFeatureType, SimpleFeature> reader = kmlFileFormat.read(featureType, IOUtils.toInputStream(kmlInput)); assertTrue("No features found", reader.hasNext()); SimpleFeature feature = reader.next(); assertNotNull("Expecting feature", feature); assertEquals("Invalid ext attr foo", 42, feature.getAttribute("foo")); }
public Collection<SimpleFeatureType> parseFeatureTypes(String typeName, File file) throws IOException { InputStream inputStream = null; try { inputStream = new FileInputStream(file); return parseFeatureTypes(typeName, inputStream); } finally { if (inputStream != null) { inputStream.close(); } } }
SimpleFeature feature = (SimpleFeature) object; SimpleFeatureType ft = feature.getFeatureType(); aggregateFeatureType = unionFeatureTypes(aggregateFeatureType, ft); Map<Object, Object> userData = feature.getUserData(); @SuppressWarnings("unchecked") featureType = unionFeatureTypes(featureType, schema); featureType = convertParsedFeatureType(featureType, typeName, untypedAttributes); if (!schemaNames.isEmpty()) { Map<Object, Object> userData = featureType.getUserData();
@Override public boolean canRead(ImportData data) throws IOException { File file = getFileFromData(data); return file.canRead() && "kml".equalsIgnoreCase(FilenameUtils.getExtension(file.getName())); }
public void testReadFeatureWithUntypedExtendedData() throws Exception { String kmlInput = DOC_EL + "<Placemark>" + "<ExtendedData>" + "<Data name=\"foo\"><value>bar</value></Data>" + "<Data name=\"quux\"><value>morx</value></Data>" + "</ExtendedData>" + "</Placemark></kml>"; SimpleFeatureType featureType = kmlFileFormat.parseFeatureTypes("foo", IOUtils.toInputStream(kmlInput)).get(0); FeatureReader<SimpleFeatureType, SimpleFeature> reader = kmlFileFormat.read(featureType, IOUtils.toInputStream(kmlInput)); assertTrue("No features found", reader.hasNext()); SimpleFeature feature = (SimpleFeature) reader.next(); assertNotNull("Expecting feature", feature); assertEquals("Invalid ext attr foo", "bar", feature.getAttribute("foo")); assertEquals("Invalid ext attr quux", "morx", feature.getAttribute("quux")); }
public void testParseFeatureTypeMinimal() throws Exception { String kmlInput = DOC_EL + "<Placemark></Placemark></kml>"; List<SimpleFeatureType> featureTypes = kmlFileFormat.parseFeatureTypes("foo", IOUtils.toInputStream(kmlInput)); assertEquals("Unexpected number of feature types", 1, featureTypes.size()); SimpleFeatureType featureType = featureTypes.get(0); assertEquals( "Unexpected number of feature type attributes", 10, featureType.getAttributeCount()); }
public void testMultipleSchemas() throws Exception { String kmlInput = DOC_EL + "<Schema name=\"schema1\">" + "<SimpleField type=\"int\" name=\"foo\"></SimpleField>" + "</Schema>" + "<Schema name=\"schema2\">" + "<SimpleField type=\"float\" name=\"bar\"></SimpleField>" + "</Schema>" + "<Placemark>" + "<ExtendedData>" + "<SchemaData schemaUrl=\"#schema1\">" + "<SimpleData name=\"foo\">42</SimpleData>" + "</SchemaData>" + "<SchemaData schemaUrl=\"#schema2\">" + "<SimpleData name=\"bar\">4.2</SimpleData>" + "</SchemaData>" + "</ExtendedData>" + "</Placemark></kml>"; List<SimpleFeatureType> featureTypes = kmlFileFormat.parseFeatureTypes("multiple", IOUtils.toInputStream(kmlInput)); assertEquals("Unexpected number of feature types", 1, featureTypes.size()); SimpleFeatureType ft = featureTypes.get(0); FeatureReader<SimpleFeatureType, SimpleFeature> reader = kmlFileFormat.read(ft, IOUtils.toInputStream(kmlInput)); SimpleFeature feature1 = reader.next(); assertNotNull("Expecting feature", feature1); assertEquals("Invalid ext attr foo", 42, feature1.getAttribute("foo")); assertEquals("Invalid ext attr bar", 4.2f, (Float) feature1.getAttribute("bar"), 0.01); }
public void testParseFeatureTypeNoPlacemarks() throws IOException { String kmlInput = DOC_EL + "</kml>"; try { kmlFileFormat.parseFeatureTypes("foo", IOUtils.toInputStream(kmlInput)); } catch (IllegalArgumentException e) { assertTrue(true); return; } fail("Expected Illegal Argument Exception for no features"); }
public void testTypedAndUntyped() throws Exception { String kmlInput = DOC_EL + "<Schema name=\"myschema\">" + "<SimpleField type=\"int\" name=\"foo\"></SimpleField>" + "</Schema>" + "<Placemark>" + "<ExtendedData>" + "<SchemaData schemaUrl=\"#myschema\">" + "<SimpleData name=\"foo\">42</SimpleData>" + "</SchemaData>" + "<Data name=\"fleem\"><value>bar</value></Data>" + "<Data name=\"quux\"><value>morx</value></Data>" + "</ExtendedData>" + "</Placemark></kml>"; List<SimpleFeatureType> featureTypes = kmlFileFormat.parseFeatureTypes( "typed-and-untyped", IOUtils.toInputStream(kmlInput)); assertEquals("Unexpected number of feature types", 1, featureTypes.size()); SimpleFeatureType featureType = featureTypes.get(0); FeatureReader<SimpleFeatureType, SimpleFeature> reader = kmlFileFormat.read(featureType, IOUtils.toInputStream(kmlInput)); SimpleFeature feature = reader.next(); assertNotNull("Expecting feature", feature); assertEquals("Invalid ext attr foo", 42, feature.getAttribute("foo")); assertEquals("bar", feature.getAttribute("fleem")); assertEquals("morx", feature.getAttribute("quux")); }
public void testExtendedUserData() throws Exception { String kmlInput = DOC_EL + "<Placemark>" + "<ExtendedData>" + "<Data name=\"foo\"><value>bar</value></Data>" + "<Data name=\"quux\"><value>morx</value></Data>" + "</ExtendedData>" + "</Placemark></kml>"; List<SimpleFeatureType> featureTypes = kmlFileFormat.parseFeatureTypes("fleem", IOUtils.toInputStream(kmlInput)); assertEquals("Unexpected number of feature types", 1, featureTypes.size()); SimpleFeatureType featureType = featureTypes.get(0); assertEquals( "Unexpected number of feature type attributes", 12, featureType.getAttributeCount()); assertEquals( "Invalid attribute descriptor", String.class, featureType.getDescriptor("foo").getType().getBinding()); assertEquals( "Invalid attribute descriptor", String.class, featureType.getDescriptor("quux").getType().getBinding()); }