package org.pujun.heatmap;

import com.vividsolutions.jts.geom.Geometry;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.json.JSONArray;
import org.json.JSONObject;
import org.opengis.feature.Feature;
import org.pujun.interp.InterpPm;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;

import static java.lang.Math.abs;

/**
 * Created by milletpu on 16/3/31.
 */
public class HeatMap {
    private String timePoint;
    private static final String CITY_PATH = "/Users/milletpu/airvis/src/main/webapp/maps/china_cities.json";

    public HeatMap(String timePoint) {
        this.timePoint = timePoint;
    }

    public void getInterpPm() throws IOException, ParseException {
        //获取全国所有城市的坐标
        InterpPm interpPm = new InterpPm(timePoint);
        FeatureJSON fj = new FeatureJSON();
        FeatureCollection fc = fj.readFeatureCollection(new FileInputStream(new File(CITY_PATH)));
        FeatureIterator iterator = fc.features();

        DrawHeatMap dg = new DrawHeatMap();     //实例化 画heatmap
        dg.init();      //初始化读入地图背景，png

        while (iterator.hasNext()) {
            Feature feature = iterator.next();
            String[] thisLocation = feature.getProperty("cp").getValue().toString().split(",");
            double thisLon = Double.parseDouble(thisLocation[0].replace("[", ""));
            double thisLat = Double.parseDouble(thisLocation[1].replace("]", ""));

            dg.drawEllipse(thisLat,thisLon);//取出全国所有城市的坐标，在地图png上画点


//            System.out.println("lat:"+ thisLat + "lon:" + thisLon);
//            System.out.println(interpPm.pm10(thisLat, thisLon));
//            System.out.println(interpPm.pm25(thisLat, thisLon));
        }
    }

    public static void main(String[] args) throws IOException, ParseException {
        HeatMap hmp = new HeatMap("2013-12-18 06:00:00");
        hmp.getInterpPm();
    }
}

@@ -627,16 +627,51 @@
 ath.abs;
+%0Aimport static java.lang.Math.ceil;
 %0A%0A/**%0A *
@@ -868,16 +868,138 @@
 s.json%22;
+%0A    private static Color maxColor = new Color(255,0,0,100);%0A    private static Color minColor = new Color(127,255,0,100);
 %0A%0A    pu
@@ -1101,17 +1101,41 @@
 InterpPm
-(
+25(double max, double min
 ) throws
@@ -1187,16 +1187,50 @@
 %E6%89%80%E6%9C%89%E5%9F%8E%E5%B8%82%E7%9A%84%E5%9D%90%E6%A0%87%0A
+        int colorR,colorG,colorB;%0A
         
@@ -1572,16 +1572,41 @@
 %E8%83%8C%E6%99%AF%EF%BC%8Cpng%0A%0A
+        Color thisColor;%0A
         
@@ -1943,16 +1943,651 @@
 , %22%22));%0A
+            double thisPm25 = interpPm.pm25(thisLat, thisLon);%0A%0A            int selectCorlor = (int) ceil((thisPm25 - min) / (max - min) * 383);%0A            if (selectCorlor %3E= max) %7B%0A                thisColor = maxColor;%0A            %7Delse if(selectCorlor %3E= 0 && selectCorlor %3C= 127)%7B%0A                thisColor = new Color(minColor.getRed()+selectCorlor,255,0,50);%0A            %7Delse if(selectCorlor %3E=128 && selectCorlor %3C= 383)%7B%0A                thisColor = new Color(255,minColor.getGreen()-(selectCorlor-128),0,50);%0A            %7Delse%7B%0A                thisColor = minColor;%0A            %7D%0A%0A            dg.graphics.setColor(thisColor);
 %0A       
@@ -2614,16 +2614,17 @@
 thisLat,
+ 
 thisLon)
@@ -2968,17 +2968,21 @@
 eatMap h
-m
+eatMa
 p = new 
@@ -3022,17 +3022,21 @@
        h
-m
+eatMa
 p.getInt
@@ -3040,20 +3040,29 @@
 InterpPm
-(
+25(200, 0
 );%0A
+%0A
     %7D%0A%7D%0A

制作heatmap第二步完成：可在png静态地图上画上所有城市的pm25的点，颜色从绿到红表示pm25从小到大。