January 2013 Posts

[Java] How to Convert .csv to Shapefile shape format (.shp)?

On my previous project, i had a request to provide a csv to shp converter feature. After googling for a while, i found a simple java code snippet, i modified some of its part so i could integrate it on my GIS project and hope it will be more flexible.

This is part of inflasi3.csv file that will be converted into shp

BANDA ACEH,5.546181947,95.32366186,0.8,1.45,1.82,-1.3,0.45,-0.23,-0.3,-0.04,0.7,-0.47,0.63,0.63

And this is my java class,

package com.baculsoft.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureStore;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;

public class Csv2Shape {

    public static void main(String[] args) throws Exception {
        File file = new File("inflasi3.csv");

        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        SimpleFeatureType TYPE = null;
        try {
            String line = reader.readLine();
            StringBuilder stringBuilder = new StringBuilder();

            String[] headers = line.split("\\,"); 
            for (String header : headers) {

            TYPE = DataUtilities.createType("Location", stringBuilder.substring(0, stringBuilder.toString().length() - 1));
            GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);

            for (line = reader.readLine(); line != null; line = reader.readLine()) {
                String split[] = line.split("\\,");

                String name = split[0]; 
                double latitude = Double.parseDouble(split[1]);
                double longitude = Double.parseDouble(split[2]);

                Object[] o = new Object[split.length+1];
                for (int i = 2; i < o.length; i++) {
                    o[i] = split[i-1];

                o[0] = factory.createPoint(new Coordinate(longitude, latitude));
                o[1] = name;

                SimpleFeature feature = SimpleFeatureBuilder.build(TYPE, o, null);
        } finally {
        File newFile = new File("inflasi4.shp");

        DataStoreFactorySpi factory = new ShapefileDataStoreFactory();

        Map<String, Serializable> create = new HashMap<String, Serializable>();
        create.put("url", newFile.toURI().toURL());
        create.put("create spatial index", Boolean.TRUE);

        ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);

        Transaction transaction = new DefaultTransaction("create");

        String typeName = newDataStore.getTypeNames()[0];
        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore;
        featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) newDataStore.getFeatureSource(typeName);

        try {
        } catch (Exception ex) {
        } finally {

And this is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


        <!-- use the latest snapshot -->
            <name>Java.net repository</name>
            <name>Open Source Geospatial Foundation Repository</name>


This is the screenshot of my viewer (geoexplorer) after i uploaded inflasi4.shp, which is the result of converting inflasi3.csv
converting from csv to shp

Hope it helps other, have fun (H)


[Oracle] How to Truncate and Clean All Your Database’s Tables

I worked with Oracle for this project, and sometime i need to truncate and clean all my oracle database’s content. So instead of truncate the tables one by one, i found a simple query to generate a simple truncate query.

select 'truncate table ' || table_name || ';' from user_tables

Hope it helped others :)


Asiasoft and Ragnarok Online 2, Downloaded 4GBs and Got Nothing.

Today i got a surprising announcement from Asiasoft, somehow Ragnarok Online 2 OpenBeta only available for FOUR South East Asia countries, despite there are eleven countries on SEA

A little bit weird because on their previous facebook status, they said that it will be a “Free to play for All”,

There was no notification whatsoever regarding country limitation for Open Beta before, and i already downloaded 4gbs installer for RO2 client. So basically i spent whole night downloading on my crappy internet connection and i got nothing Hahahaaa.

And i guess it’s just their way of saying "Asgardians! Thank you for helping us out in the stress testing! We have found a problem to solve, and we can't solve it. So we are now going to IP Block most of you instead.".