@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); }
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")); } }
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")); }
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 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 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); }
@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); }
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")); }