반응형

네비게이션의 gps 좌표를 선으로 이은 shp 파일 만들기


gps 좌표를 가지고 있는 csv 파일을 불러와서


좌표를 이용하여 shp 파일과 shx 파일을 생성한다


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import org.geotools.data.shapefile.shp.ShapeType;
import org.geotools.data.shapefile.shp.ShapefileWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
/**
 * shp & shx file write from gps coordinates.
 *
 * @author Haneul, Kim
 */
public class ShpWriterTest {
 
    private ShapefileWriter writer;
 
    @Before
    public void setUp() throws IOException {
        File shpFile = new File("/Users/hskimsky/Downloads/shp/GPRMC.shp");
        File shxFile = new File("/Users/hskimsky/Downloads/shp/GPRMC.shx");
 
        FileOutputStream shpFOS = new FileOutputStream(shpFile);
        FileOutputStream shxFOS = new FileOutputStream(shxFile);
 
        this.writer = new ShapefileWriter(shpFOS.getChannel(), shxFOS.getChannel());
    }
 
    @After
    public void close() throws IOException {
        this.writer.close();
    }
 
    @Test
    public void shpWrite() throws IOException {
        Scanner scanner = new Scanner(new File("/Users/hskimsky/Downloads/my_gps_GPRMC.txt"));
        List<Coordinate> coords = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] values = line.split(",");
            double[] xy = {Double.parseDouble(values[1]), Double.parseDouble(values[2])};
            coords.add(new Coordinate(xy[0], xy[1]));
        }
 
        GeometryFactory gf = new GeometryFactory();
        LineString lineString = gf.createLineString(coords.toArray(new Coordinate[coords.size()]));
        MultiLineString mls = gf.createMultiLineString(new LineString[]{lineString});
        System.out.println("mls = " + mls);
 
        this.writer.writeHeaders(mls.getEnvelopeInternal(), ShapeType.ARC, mls.getNumGeometries(), 100);
        this.writer.writeGeometry(mls);
    }
}


반응형

'[프로그래밍] > [GIS]' 카테고리의 다른 글

geotools csv to shp  (0) 2015.11.11
Posted by FeliZ_하늘..
,