Skip to content

Commit

Permalink
fix: parsing geometry with leading single quotes
Browse files Browse the repository at this point in the history
as locationtech/jts parser has either gotten stricter or not reported
such parser errors before locationtech/jts#786
  • Loading branch information
teleivo committed Jul 29, 2022
1 parent 22cc1ed commit f499f65
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.dxf2.events.event.DataValue;
Expand Down Expand Up @@ -61,6 +62,8 @@ public class DefaultCsvEventService
private static final CsvSchema CSV_SCHEMA = CSV_MAPPER.schemaFor( CsvEventDataValue.class )
.withLineSeparator( "\n" );

private static final Pattern TRIM_SINGLE_QUOTES = Pattern.compile( "^'|'$" );

@Override
public void writeEvents( OutputStream outputStream, List<Event> events, boolean withHeader )
throws IOException
Expand Down Expand Up @@ -150,7 +153,8 @@ public List<Event> readEvents( InputStream inputStream, boolean skipFirst )

if ( StringUtils.isNotBlank( dataValue.getGeometry() ) )
{
event.setGeometry( new WKTReader().read( dataValue.getGeometry() ) );
event.setGeometry( new WKTReader()
.read( TRIM_SINGLE_QUOTES.matcher( dataValue.getGeometry() ).replaceAll( "" ) ) );
}
else if ( dataValue.getLongitude() != null && dataValue.getLatitude() != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -63,6 +64,8 @@ public class TrackerCsvEventService
{
private static final CsvMapper CSV_MAPPER = new CsvMapper().enable( CsvParser.Feature.WRAP_AS_ARRAY );

private static final Pattern TRIM_SINGLE_QUOTES = Pattern.compile( "^'|'$" );

@Override
public void writeEvents( OutputStream outputStream, List<Event> events, boolean withHeader )
throws IOException
Expand Down Expand Up @@ -195,7 +198,8 @@ public List<Event> readEvents( InputStream inputStream, boolean skipFirst )

if ( StringUtils.isNotBlank( dataValue.getGeometry() ) )
{
event.setGeometry( new WKTReader().read( dataValue.getGeometry() ) );
event.setGeometry( new WKTReader()
.read( TRIM_SINGLE_QUOTES.matcher( dataValue.getGeometry() ).replaceAll( "" ) ) );
}
else if ( dataValue.getLongitude() != null && dataValue.getLatitude() != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand All @@ -46,6 +48,11 @@
import org.hisp.dhis.webapi.controller.tracker.view.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.io.ParseException;

import com.google.common.io.Files;
Expand All @@ -55,6 +62,8 @@ class TrackerCsvEventServiceTest

private TrackerCsvEventService service;

private GeometryFactory geometryFactory;

@BeforeEach
void setUp()
{
Expand Down Expand Up @@ -209,4 +218,26 @@ void testReadEventsFromFileWithoutHeader()
assertFalse( dv.isProvidedElsewhere() );
} );
}

@ValueSource( strings = {
",,,,,,,,,POINT (-11.4283223849698 8.06311527044516)",
",,,,,,,,,\"POINT (-11.4283223849698 8.06311527044516)\"",
",,,,,,,,,'POINT (-11.4283223849698 8.06311527044516)'",
} )
@ParameterizedTest
void testReadEventsParsesGeometryEvenIfQuoted( String csv )
throws IOException,
ParseException
{

InputStream in = new ByteArrayInputStream( csv.getBytes( StandardCharsets.UTF_8 ) );

List<Event> events = service.readEvents( in, false );

assertFalse( events.isEmpty() );
assertEquals( 1, events.size() );
geometryFactory = new GeometryFactory();
Point expected = geometryFactory.createPoint( new Coordinate( -11.4283223849698, 8.06311527044516 ) );
assertEquals( expected, events.get( 0 ).getGeometry() );
}
}

0 comments on commit f499f65

Please sign in to comment.