Javafx openData ("Kenya Natural Disaster 1997 to 2013")

As usual playing around with data from kenya opendata portal.I avoid PDF format cause of too much copying and pasting after extracting to a text file.Which i still believe PDF is not 100% open data.

Last week Natural Disasters from 1999 to 2013 data was uploaded and in csv format,after working with the data i found out that the data dates back from 1997 and not 1999,more worse is that the column count differed in some lines,the column count differed from 42 to 52;.

Thanks for the data really appreciate it.

Showing all events

Getting the coordinates

If the column count is 42 the index of the latitude is 36 and longitude index is 37; or 36+offset

The method setIndex checks for column count and assign an index.

 private int setIndex(int columnLength,int offset){
        switch(columnLength){
            case 42:
                return 36+offset;
            case 43:
                return 38+offset;
            case 44:
                return 38+offset;
            case 45:
                return 39+offset;
            case 46:
                return 40+offset;
            case 47:
                return 41+offset;
            case 48:
                return 42+offset;
            case 49:
                return 43+offset;
            case 50:
                return 44+offset;
            case 51:
                return 45+offset;
            case 52:
                return 46+offset;
            default:
                return 0;
        }

 Selecting Lines from matching dates

 List<String> lines=FileData.getLines();

ObservableList<String>    years=FXCollections.observableArrayList("1997","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013");

ComboBox combo=new ComboBox(years);

 final String regex="\\d{2}/\\d{2}/";

The date column is at index 4

combo.valueProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue ov, String t, String t1) {
     
             for(int i=1;i<lines.size();i++){
         
              String date=lines.get(i).split(",")[4].split("\\s")[0];//date i.e 01/14/2010
       
              if(date.matches(regex+t1)){
           
                  int length=lines.get(i).split(",").length;
                   
                  double lat=Double.valueOf(lines.get(i).split(",")[checkIndex(length,0)].substring(2));
                  double lon=Double.valueOf(lines.get(i).split(",")[checkIndex(length,1)].substring(0,6));

              }

           }
The AreaChart

The chart show the frequency of disasters over the years.
I took the date from index 4 and remove the month and day remaining with the year only,adding them to a List.

 private List getYears(){
     
       List<String> data=new ArrayList<>();

         for(int i=1;i<lines.size();i++){
             String rry[]=lines.get(i).split(",");
             data.add(rry[4].split("\\s")[0].substring(6,10));//i.e 2009
        }
       return data;
    }
Populating the chart

        List list=getYears();
     
        AreaChart<String,Number> chart=new AreaChart<>(xAxis,yAxis);
        xAxis.invalidateRange(years);
        XYChart.Series series  = new XYChart.Series();
     
        for(int i=0;i<years.size();i++){
           series.getData().add(new XYChart.Data(years.get(i),Collections.frequency(list,years.get(i))));
        }
   chart.getData().add(series);

The map

The map is from the arcgis java sdk.

       JMap map=new JMap(new MapOptions(MapType.GRAY_BASE));
       map.setBackground(Color.gray);
       map.setDoubleBuffered(true);
       map.setWrapAroundEnabled(true);
       map.setShowingEsriLogo(false);

     GraphicsLayer  gl=new GraphicsLayer();
     
      map.setSize(850,650);
       
      map.getLayers().add(gl);
     
      NavigatorOverlay navigatorOverlay = new NavigatorOverlay();
      map.addMapOverlay(navigatorOverlay);

//add markers method
   public void addMarkers(double lat,double lon,BufferedImage image){
       map.addMarkerGraphic(lat,lon,"", "","","",image);
    }

Results


Comments

Popular Posts