Skip to content

Commit

Permalink
deegree#849 - writing of single features (PR285)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoltz committed Jul 29, 2021
1 parent b5316f8 commit 69e4982
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.deegree.geojson;

import org.deegree.cs.exceptions.TransformationException;
import org.deegree.cs.exceptions.UnknownCRSException;
import org.deegree.feature.Feature;

import java.io.IOException;

/**
* Writer for GeoJSON documents with single features.
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
public interface GeoJsonSingleFeatureWriter {

/**
* Starts a single feature.
* <p>
* To finish the feature call endSingleFeature
*
* @throws IOException
* if GeoJSON could no be written
*/
void startSingleFeature()
throws IOException;

/**
* Ends the written feature.
* <p>
* Ensure that startSingleFeature was called before.
*
* @throws IOException
* if GeoJSON could no be written
*/
void endSingleFeature()
throws IOException;

/**
* Writes a new, single feature.
*
* @param feature
* the feature to write, never <code>null</code>
* @throws IOException
* if GeoJSON could no be written
* @throws TransformationException
* if a geometry to export cannot be transformed to CRS:84
* @throws UnknownCRSException
* if the CRS of the geometry is not supported
*/
void writeSingleFeature( Feature feature )
throws IOException, UnknownCRSException, TransformationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*
* @author <a href="mailto:goltz@lat-lon.de">Lyn Goltz </a>
*/
public class GeoJsonWriter extends JsonWriter implements GeoJsonFeatureWriter {
public class GeoJsonWriter extends JsonWriter implements GeoJsonFeatureWriter, GeoJsonSingleFeatureWriter {

private static final Logger LOG = LoggerFactory.getLogger( GeoJsonWriter.class );

Expand Down Expand Up @@ -90,6 +90,20 @@ public void endFeatureCollection()
endObject();
}

@Override
public void startSingleFeature()
throws IOException {
isStarted = true;
beginObject();
name( "type" ).value( "Feature" );
}

@Override
public void endSingleFeature()
throws IOException {
endObject();
}

@Override
public void write( Feature feature )
throws IOException, TransformationException, UnknownCRSException {
Expand All @@ -100,13 +114,18 @@ public void write( Feature feature )
name( "features" ).beginArray();
isStarted = true;
}
beginObject();
name( "type" ).value( "Feature" );
startSingleFeature();
writeSingleFeature( feature );
endSingleFeature();
}

@Override
public void writeSingleFeature( Feature feature )
throws IOException, UnknownCRSException, TransformationException {
name( "id" ).value( feature.getId() );
writeGeometry( feature );
writeProperties( feature );
writeCrs();
endObject();
}

private void writeGeometry( Feature feature )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,41 @@ public void testWrite()

}

@Test
public void testWrite_SingleFeature()
throws Exception {
StringWriter featureAsJson = new StringWriter();
GeoJsonWriter geoJsonFeatureWriter = new GeoJsonWriter( featureAsJson, null );
Feature cadastralZoning = parseFeature( "CadastralZoningWithPrimitiveArray.gml" );

geoJsonFeatureWriter.startSingleFeature();
geoJsonFeatureWriter.writeSingleFeature( cadastralZoning );
geoJsonFeatureWriter.endSingleFeature();

String featureCollection = featureAsJson.toString();

assertThat( featureCollection, JsonPathMatchers.isJson() );
assertThat( featureCollection, hasJsonPath( "$.type", is( "Feature" ) ) );
assertThat( featureCollection, hasNoJsonPath( "$.srsName" ) );
assertThat( featureCollection, hasJsonPath( "$.id", is( "CP_CADASTRALZONING_Bundesland_02" ) ) );
assertThat( featureCollection, hasJsonPath( "$.geometry" ) );
assertThat( featureCollection, hasJsonPath( "$.properties.label", hasItem( "01" ) ) );
assertThat( featureCollection, hasJsonPath( "$.properties.label", hasItem( "02" ) ) );
assertThat( featureCollection,
hasJsonPath( "$.properties.originalMapScaleDenominator", is( 10 ) ) );
assertThat( featureCollection,
hasJsonPath( "$.properties.beginLifespanVersion", is( "2009-12-15T08:04:54Z" ) ) );
assertThat( featureCollection, hasJsonPath( "$.properties.estimatedAccuracy.uom", is( "m" ) ) );
assertThat( featureCollection, hasJsonPath( "$.properties.inspireId.Identifier.localId",
is( "urn:adv:oid:DEHHALKA10000005" ) ) );
assertThat( featureCollection,
hasJsonPath( "$.properties.name.GeographicalName.spelling.SpellingOfName.text",
is( "Hamburg" ) ) );
assertThat( featureCollection, hasJsonPath( "$.properties.levelName.LocalisedCharacterString.value",
is( "Bundesland" ) ) );
}


@Test
public void testWriteWithCrs()
throws Exception {
Expand Down

0 comments on commit 69e4982

Please sign in to comment.