import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class LoadToCsv {
public static void main(String[] args) {
String csvPipeDivide = "\\|"; // 구분자
try {
File csvFile = new File("C:\\Users\\king\\Desktop\\shapeFile\\a.csv");
BufferedReader br = new BufferedReader((new FileReader(csvFile)));
int count = 0;
String line = null;
while((line=br.readLine())!=null) {
count = count+1;
String[] value = line.split(csvPipeDivide);
System.out.println(Arrays.toString(value));
}
//System.out.println(count);
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Posted by kingroad

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;

public class App {

public static void main(String[] args) throws IOException {
String anotherInputFilePath = "C:\\Users\\king\\Desktop\\shapeFile\\a.shp";
String anotherOutputFilePath = "C:\\Users\\king\\Desktop\\shapeFile\\a.csv";

BufferedWriter bw = new BufferedWriter(new FileWriter(anotherOutputFilePath));

loadToShp loadShape = new loadToShp();
loadShape.DefaultLoad(anotherInputFilePath);
StringBuffer tmpStr = new StringBuffer();

List properties = (List) loadShape.getIter().next().getProperties();
// 프로퍼티 명
for (int i = 0; i < properties.size(); i++) {
if(i==(properties.size()-1)) {
tmpStr.append(properties.get(i).getName()).append("\n");
} else {
tmpStr.append(properties.get(i).getName()).append("|");
}
//tmpStr.append("\n");
//System.out.printf("%d번째 진행중",i);
}
bw.write(tmpStr.toString());
// 프로퍼티 명 쓰기 End

 

// 레코드를 파일에 쓰기
int x=0;

//파일이 너무 커서 레코드의 갯수를 저장하기용 또는 레코드가 잘 써지는지 10만줄에 따라 print해보기용

while(loadShape.getIter().hasNext()) {
if((x%100000)==0) {
System.out.println(tmpStr.toString());
}

//if(x==10){

//bw.close(); 
//break; }


tmpStr = new StringBuffer();
SimpleFeature f = loadShape.getIter().next();

for(int i=0; i<f.getAttributeCount(); i++) {
if(i==(f.getAttributeCount()-1)) {
tmpStr.append(f.getAttribute(i).toString()).append("\n");
} else {
tmpStr.append(f.getAttribute(i).toString()).append("|");
}
}
//System.out.println(tmpStr.toString());
bw.write(tmpStr.toString());
x++;

}
bw.close();
}
}

 

// csv file이 잘 써졋는지 확인을 하기 위해서 excel로 csv파일을 열었을 때 변환하려는 원본 파일이 너무 크다면

// 파일 용량은 증가했으나 백지로 열리는 경우가 있는데

// excel->데이터 항목->데이터 가져오기->파일에서->텍스트/csv->저장된 excel 파일 클릭->구분기호 지정 ex) | 또는 ,

// ->로드 순으로 열어서 확인해볼 수 있다

// 또한 데이터가 일치하지 않아 보일 수 있는데 excel의 문제이다. 파일 레코드를 소량만 변환하여 notepad++와 같은 // 다른 도구를 사용하여 확인을 해보면 정상적으로 변환됨을 확인할 수 있다. 

Posted by kingroad

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;


public class loadToShp {
private static SimpleFeatureIterator simpleFeatureIterator;

public void DefaultLoad(String filePath) {
// Open the shapefile
File dataFile = new File(filePath);

ShapefileDataStore store;

try {
store = new ShapefileDataStore(new URL("file://" + dataFile));
store.setCharset(Charset.forName("UTF-8"));
SimpleFeatureSource source = store.getFeatureSource(store.getTypeNames()[0]); 

//file의 이름을 인자로 하여 그 이름에 해당된 정보를 가지고 옴
SimpleFeatureCollection featureCollection = source.getFeatures();
simpleFeatureIterator = featureCollection.features();

} catch (IOException e) {
e.printStackTrace();
}
}

public SimpleFeatureIterator getIter() {
return simpleFeatureIterator;
}
}

Posted by kingroad

블로그 이미지
개발자를 꿈꾸는 코린이 입니다
kingroad

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.5
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

최근에 올라온 글

최근에 달린 댓글

글 보관함