shp2csv

File inFile = new File("/home/ian/Data/states/states.shp");
File outFile = new File("states.csv");
outFile.createNewFile();
// Read
DataStore inputDataStore = DataStoreFinder.getDataStore(
        Collections.singletonMap("url", URLs.fileToUrl(inFile)));

String inputTypeName = inputDataStore.getTypeNames()[0];
SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);

FeatureSource<SimpleFeatureType, SimpleFeature>
        source = inputDataStore.getFeatureSource(inputTypeName);

FeatureCollection<SimpleFeatureType, SimpleFeature>
        inputFeatureCollection = source.getFeatures();

// Write
FileDataStore newDataStore = FileDataStoreFinder.getDataStore(outFile);

newDataStore.createSchema(inputType);
String typeName = newDataStore.getTypeNames()[0];

SimpleFeatureStore featureStore =
        (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);

featureStore.addFeatures(inputFeatureCollection);
StringBuffer tmpStr = new StringBuffer();
tmpStr.append("ID,date,tstamp,X_prj,Y_prj,NEAR_FID,NEAR_DIST\n");

SimpleFeature f = null;
int index = 0;
String ID = null;

while(simpleFeatureIterator.hasNext()){
        f = simpleFeatureIterator.next();
        index = f.getID().lastIndexOf('.');
        ID = f.getID().substring(index+1);

        tmpStr.append(Integer.toString(Integer.parseInt(ID))).append(",")
                .append(f.getAttribute(1).toString()).append(",")
                .append(f.getAttribute(2).toString()).append(",")
                .append(f.getAttribute(3).toString()).append(",")
                .append(f.getAttribute(4).toString()).append(",")
                .append(f.getAttribute(5).toString()).append(",")
                .append(f.getAttribute(6).toString()).append("\n");
}

BufferedWriter bw = new BufferedWriter (new FileWriter(dest_path+dest_filename));
bw.write(tmpStr.toString());
bw.close();
package io.pivotal.pde;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.serialization.StringSerializer;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.Bindings;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.integration.annotation.Splitter;
import org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler;
import org.springframework.integration.kafka.support.KafkaProducerContext;
import org.springframework.integration.kafka.support.ProducerConfiguration;
import org.springframework.integration.kafka.support.ProducerFactoryBean;
import org.springframework.integration.kafka.support.ProducerMetadata;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ByteOrderValues;
import com.vividsolutions.jts.io.WKBWriter;

@EnableBinding(Processor.class)
@SpringBootApplication
public class Shp2CsvProcessor {

    private static Logger LOG = LoggerFactory.getLogger(Shp2CsvProcessor.class);
    private static final String NAME = "Shp2CsvProcessor";
    
    private MessageChannel output;
 
    @Autowired
    public void Shp2CsvProcessor(MessageChannel output) {
        this.output = output;
    }
    
    @Splitter(inputChannel = Processor.INPUT)
    public void transform(File f) throws Exception {
        // Note, the .dbf needs to be accessible in
        // the same path for the features to be found

        String columnDelimiter = "|";
        String rowDelimiter = "\n";

        Map<String, Object> map = new HashMap<String, Object>();
        URL theUrl = f.toURI().toURL();
        map.put("url", theUrl);
        
        LOG.info("Resolved URL : {}", theUrl);

        DataStore dataStore = DataStoreFinder.getDataStore(map);
        
        if (dataStore == null) {
            LOG.error("DataStore not created for url : {}", theUrl);
            Iterator<DataStoreFactorySpi> availableStores =  DataStoreFinder.getAvailableDataStores();
            LOG.info("List available Stores: {");
                        while (availableStores.hasNext()) {
                            LOG.info(availableStores.next().toString());
                        }
            LOG.info("}");
            throw new Exception();
        }
        
        String typeName = dataStore.getTypeNames()[0];
        LOG.info("DataStore Mapper : Status={}", "DONE");

        FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
        LOG.info("Features Mapped : Status={}", "DONE");
        FeatureIterator<SimpleFeature> features = collection.features();

        WKBWriter wkbWriter; 

        try {
            while(features.hasNext()){
                SimpleFeature feature = features.next();

                List<String> rowStringList = new ArrayList<String>();

                Boolean isFirst = true;
                for (Object thisAttr : feature.getAttributes()) {
                    // Skip the first attribute (geometry column)
                    // it is moved to the end of the row string
                    if (isFirst) {
                        isFirst = false;
                        continue;
                    }
                    rowStringList.add(thisAttr.toString());
                }

                Geometry geom = (Geometry) feature.getDefaultGeometry();
                Integer dimensions = geom.getDimension();
                // the WKB writer needs at least 2 dimensions
                // MULTILINESTRINGS are realistically 2D anyway
                if (dimensions == 1) {
                    dimensions = 2;
                }
                //LOG.info("Dimensions {} ", geom.getDimension());

                wkbWriter = new WKBWriter(dimensions,  ByteOrderValues.LITTLE_ENDIAN); 
                String wkbHexString = WKBWriter.toHex(wkbWriter.write(geom));

                rowStringList.add(wkbHexString);

                // Convert the column list into a delimited string
                String thisRow = StringUtils.join(rowStringList, columnDelimiter) + rowDelimiter;
                
                output.send(MessageBuilder.withPayload(thisRow).build());
            }

        } catch (Exception e) {
            LOG.error("Exception in transformer={} ; Exception={}", NAME, e);
            throw new Exception();
        } finally {
            features.close();
            dataStore.dispose();
        }

        LOG.info("Transformed by {} : Status={}", NAME, "DONE");
        LOG.info("Status={}", "SUCCESS");
    }

    public static void main(String[] args) {
        SpringApplication.run(Shp2CsvProcessor.class, args);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值