package usda.weru.util.gis;

import java.io.DataInputStream;
import java.io.EOFException;
import de.schlichtherle.truezip.file.TFileInputStream;
import java.io.IOException;
import java.util.Vector;
import usda.weru.util.Util;

/**
 *
 * @author wjr
 */
public class ReadShapefile {

    Vector<HoleyPolygon> V_hp = new Vector<HoleyPolygon>();

	/**
	 *
	 * @param fileName
	 */
	public ReadShapefile(String fileName) {
        DataInputStream in = null;
        int multiPartPolygons = 0;

//        ReadShapefile rsf = new ReadShapefile();
        ReadDBFFile dbf = new ReadDBFFile(fileName);

        try {
            fileName = Util.purgeExtensions(fileName, Util.getExtensions(fileName));
            if (!fileName.endsWith(".shp")) {
                fileName += ".shp";
            }
            in = new DataInputStream(new TFileInputStream(fileName));
            FileHeader fh = new FileHeader();
            fh.read(in);
//            System.out.println(fh);
            int ddx = 0;
            while (true) {
                RecordHeader rh = new RecordHeader();
                String dbfStr = dbf.getRecord(ddx++);
                rh.read(in);
//                System.out.println(rh + " avail " + in.available());
                if (rh.recordType != 5) {
                    in.skipBytes(rh.contentLength - 4);
                } else {
                    HoleyPolygon hp = new HoleyPolygon(in);
                    hp.setDBFStr(dbf, dbfStr);
//                    System.out.println(hp);
                    if (hp.numParts > 1) {
                        multiPartPolygons++;
                    }
                    V_hp.add(hp);
                }
            }
        } catch (EOFException eofe) {
//            System.out.println("done " + multiPartPolygons);
        } catch (IOException e) {
            //System.err.println("RunFileData: " + e);
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
    }

	/**
	 *
	 * @param lat
	 * @param lon
	 * @return
	 */
	public String findHoleyPolygon(double lat, double lon) {
        for (HoleyPolygon hp : V_hp) {
            if (hp.contains(lat, lon)) return hp.getState() + "|" + hp.getStation();
        }
        return null;
    }

	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
        if (args.length == 0) {
            args = new String[1];
            args[0] = "db/wind_gen/WINGEN_20090508/WINGEN_20090508";
//            args[0] = "db/cligen/CLIGEN_20090508_geo/CLIGEN_20090508_geo";
//            args[0] = "db/cligen/shapetest/CLIGEN_areas_04_04_08_DD_NAD83";
//            args[0] = "data/CLIGEN_areas_04_04_08/CLIGEN_areas_04_04_08";
//            args[0] = "data/CLIGEN_areas_04_04_08/CLIGEN_areas_04_04_08_reprojected.shp";
//            args[0] = "C:/weps.svn/GISViewer/uDig/data-v1_1/cities.shp";
//            args[0] = "C:/weps.svn/GISViewer/uDig/data-v1_1/countries.shp";
        }
        ReadShapefile rs = new ReadShapefile(args[0]);
        System.out.println(rs.findHoleyPolygon(44.75, -108.75));
        
        HoleyPolygon hp2 = rs.V_hp.get(2);
        System.out.println("" + hp2.getBounds());
        
        for (HoleyPolygon hp : rs.V_hp) {
            double x = hp.getStationLatLon().getX() * 100000;
            double y = hp.getStationLatLon().getY() * 100000;
            System.out.printf(">> %s %8.3f %8.3f --->>> %s\n", hp.getStation(), x, y,
                    rs.findHoleyPolygon(x,y));
        }
        
    }
}
