Skip to content

Commit 5d93e85

Browse files
authored
Merge pull request #1181 from jampukka/fix/mif-parser
MIF file support: TEXT object with "textstring" on separate line
2 parents 3038ba5 + efe6327 commit 5d93e85

3 files changed

Lines changed: 133 additions & 59 deletions

File tree

geotools-ext/gt-mif/src/main/java/org/geotools/mif/MIFDataReader.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,17 @@ private LineString parseArc(String line) throws IOException {
253253
}
254254

255255
private Point parseText(String line) throws IOException {
256-
// Skip TEXT line
257-
line = mif.poll();
256+
if (line.equalsIgnoreCase("TEXT")) {
257+
// TEXT
258+
// "textstring"
259+
line = mif.poll();
260+
line = mif.poll();
261+
} else {
262+
// TEXT "textstring"
263+
line = mif.poll();
264+
}
258265

266+
// x1 y1 x2 y2
259267
String[] a = line.split("\\s+");
260268
double x1 = Double.parseDouble(a[1]);
261269
double y1 = Double.parseDouble(a[2]);

geotools-ext/gt-mif/src/test/java/org/geotools/mif/MIFFeatureReaderTest.java

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,70 +17,67 @@
1717

1818
import org.locationtech.jts.geom.CoordinateSequence;
1919
import org.locationtech.jts.geom.MultiPolygon;
20+
import org.locationtech.jts.geom.Point;
2021
import org.locationtech.jts.geom.Polygon;
2122

2223
public class MIFFeatureReaderTest {
2324

2425
@Test
2526
public void testReadingEmptyMIDFields() throws URISyntaxException, IOException {
26-
try {
27-
File mif = new File(getClass().getResource("empty_fields.MIF").toURI());
28-
File mid = new File(getClass().getResource("empty_fields.MID").toURI());
29-
DataStore store = new MIFDataStore(mif, mid);
30-
SimpleFeatureSource source = store.getFeatureSource("empty_fields");
31-
SimpleFeatureCollection fc = source.getFeatures();
32-
Assertions.assertEquals(1, fc.size());
33-
try (SimpleFeatureIterator it = fc.features()) {
34-
if (!it.hasNext()) {
35-
Assertions.fail();
36-
}
37-
SimpleFeature f = it.next();
38-
39-
/*
40-
Region 1
41-
4
42-
357517.2 6860602.8
43-
357539.1 6860613.8
44-
357556.1 6860578.1
45-
357533.8 6860567.8
46-
*/
47-
Polygon region = (Polygon) f.getDefaultGeometry();
48-
Assertions.assertEquals(5, region.getNumPoints()); // +1 because ring is automatically closed
49-
Assertions.assertEquals(0, region.getNumInteriorRing());
50-
51-
CoordinateSequence csq = region.getExteriorRing().getCoordinateSequence();
52-
Assertions.assertEquals(357517.2, csq.getOrdinate(0, 0), 1e-9);
53-
Assertions.assertEquals(6860602.8, csq.getOrdinate(0, 1), 1e-9);
54-
Assertions.assertEquals(357539.1, csq.getOrdinate(1, 0), 1e-9);
55-
Assertions.assertEquals(6860613.8, csq.getOrdinate(1, 1), 1e-9);
56-
Assertions.assertEquals(357556.1, csq.getOrdinate(2, 0), 1e-9);
57-
Assertions.assertEquals(6860578.1, csq.getOrdinate(2, 1), 1e-9);
58-
Assertions.assertEquals(357533.8, csq.getOrdinate(3, 0), 1e-9);
59-
Assertions.assertEquals(6860567.8, csq.getOrdinate(3, 1), 1e-9);
60-
Assertions.assertEquals(357517.2, csq.getOrdinate(4, 0), 1e-9);
61-
Assertions.assertEquals(6860602.8, csq.getOrdinate(4, 1), 1e-9);
62-
63-
Assertions.assertNull(f.getAttribute("id"));
64-
Assertions.assertEquals(Integer.class, f.getProperty("id").getType().getBinding());
65-
66-
Assertions.assertNull(f.getAttribute("foo"));
67-
Assertions.assertEquals(Long.class, f.getProperty("foo").getType().getBinding());
68-
69-
Assertions.assertNull(f.getAttribute("bar"));
70-
Assertions.assertEquals(Float.class, f.getProperty("bar").getType().getBinding());
71-
72-
Assertions.assertNull(f.getAttribute("baz"));
73-
Assertions.assertEquals(Double.class, f.getProperty("baz").getType().getBinding());
74-
75-
Assertions.assertNull(f.getAttribute("qux"));
76-
Assertions.assertEquals(Boolean.class, f.getProperty("qux").getType().getBinding());
77-
78-
if (it.hasNext()) {
79-
Assertions.fail();
80-
}
27+
File mif = new File(getClass().getResource("empty_fields.MIF").toURI());
28+
File mid = new File(getClass().getResource("empty_fields.MID").toURI());
29+
DataStore store = new MIFDataStore(mif, mid);
30+
SimpleFeatureSource source = store.getFeatureSource("empty_fields");
31+
SimpleFeatureCollection fc = source.getFeatures();
32+
Assertions.assertEquals(1, fc.size());
33+
try (SimpleFeatureIterator it = fc.features()) {
34+
if (!it.hasNext()) {
35+
Assertions.fail();
36+
}
37+
SimpleFeature f = it.next();
38+
39+
/*
40+
Region 1
41+
4
42+
357517.2 6860602.8
43+
357539.1 6860613.8
44+
357556.1 6860578.1
45+
357533.8 6860567.8
46+
*/
47+
Polygon region = (Polygon) f.getDefaultGeometry();
48+
Assertions.assertEquals(5, region.getNumPoints()); // +1 because ring is automatically closed
49+
Assertions.assertEquals(0, region.getNumInteriorRing());
50+
51+
CoordinateSequence csq = region.getExteriorRing().getCoordinateSequence();
52+
Assertions.assertEquals(357517.2, csq.getOrdinate(0, 0), 1e-9);
53+
Assertions.assertEquals(6860602.8, csq.getOrdinate(0, 1), 1e-9);
54+
Assertions.assertEquals(357539.1, csq.getOrdinate(1, 0), 1e-9);
55+
Assertions.assertEquals(6860613.8, csq.getOrdinate(1, 1), 1e-9);
56+
Assertions.assertEquals(357556.1, csq.getOrdinate(2, 0), 1e-9);
57+
Assertions.assertEquals(6860578.1, csq.getOrdinate(2, 1), 1e-9);
58+
Assertions.assertEquals(357533.8, csq.getOrdinate(3, 0), 1e-9);
59+
Assertions.assertEquals(6860567.8, csq.getOrdinate(3, 1), 1e-9);
60+
Assertions.assertEquals(357517.2, csq.getOrdinate(4, 0), 1e-9);
61+
Assertions.assertEquals(6860602.8, csq.getOrdinate(4, 1), 1e-9);
62+
63+
Assertions.assertNull(f.getAttribute("id"));
64+
Assertions.assertEquals(Integer.class, f.getProperty("id").getType().getBinding());
65+
66+
Assertions.assertNull(f.getAttribute("foo"));
67+
Assertions.assertEquals(Long.class, f.getProperty("foo").getType().getBinding());
68+
69+
Assertions.assertNull(f.getAttribute("bar"));
70+
Assertions.assertEquals(Float.class, f.getProperty("bar").getType().getBinding());
71+
72+
Assertions.assertNull(f.getAttribute("baz"));
73+
Assertions.assertEquals(Double.class, f.getProperty("baz").getType().getBinding());
74+
75+
Assertions.assertNull(f.getAttribute("qux"));
76+
Assertions.assertEquals(Boolean.class, f.getProperty("qux").getType().getBinding());
77+
78+
if (it.hasNext()) {
79+
Assertions.fail();
8180
}
82-
} catch (Exception e) {
83-
e.printStackTrace();
8481
}
8582
}
8683

@@ -170,4 +167,49 @@ public void testFeatureReader() throws URISyntaxException, IOException {
170167
}
171168
}
172169

170+
@Test
171+
public void testReadingTextstringOnSeparateLine() throws URISyntaxException, IOException {
172+
File mif = new File(getClass().getResource("textstring_separate_line.mif").toURI());
173+
File fakeMid = new File("anything_that_doesnt_exists");
174+
175+
DataStore store = new MIFDataStore(mif, fakeMid);
176+
SimpleFeatureSource source = store.getFeatureSource("textstring_separate_line");
177+
SimpleFeatureCollection fc = source.getFeatures();
178+
Assertions.assertEquals(2, fc.size());
179+
try (SimpleFeatureIterator it = fc.features()) {
180+
if (!it.hasNext()) {
181+
Assertions.fail();
182+
}
183+
SimpleFeature f = it.next();
184+
185+
/*
186+
REGION 1
187+
8
188+
321129.582 7003475.008
189+
319770.969 7003405.009
190+
319492.514 7003390.491
191+
319479.797 7003389.828
192+
318699.691 7003352.012
193+
318696.539 7003426.558
194+
321120.18 7003556.465
195+
321129.582 7003475.008
196+
BRUSH(1,0)
197+
*/
198+
Polygon region = (Polygon) f.getDefaultGeometry();
199+
Assertions.assertEquals(8, region.getNumPoints());
200+
Assertions.assertEquals(0, region.getNumInteriorRing());
201+
202+
/*
203+
TEXT
204+
"233-404-5-70"
205+
319350.402 7003400.612 319415.202 7003405.612
206+
*/
207+
if (!it.hasNext()) {
208+
Assertions.fail();
209+
}
210+
f = it.next();
211+
Assertions.assertEquals(Point.class, f.getDefaultGeometry().getClass());
212+
}
213+
}
214+
173215
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
VERSION 450
2+
CHARSET "ISO8859_1"
3+
DELIMITER ","
4+
CoordSys Earth Projection 8, 115, "m", 27, 0, 0.9996, 500000, 0
5+
COLUMNS 4
6+
id Integer
7+
tekstipisteteksti Char(20)
8+
kiinteistotunnus Char(20)
9+
kuntatunnus Char(3)
10+
DATA
11+
REGION 1
12+
8
13+
321129.582 7003475.008
14+
319770.969 7003405.009
15+
319492.514 7003390.491
16+
319479.797 7003389.828
17+
318699.691 7003352.012
18+
318696.539 7003426.558
19+
321120.18 7003556.465
20+
321129.582 7003475.008
21+
BRUSH(1,0)
22+
TEXT
23+
"233-404-5-70"
24+
319350.402 7003400.612 319415.202 7003405.612

0 commit comments

Comments
 (0)