Blame view
src/collembola/MapMain.java
6.57 KB
1b1e928cc initial import of... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
package collembola; import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Random; import javax.swing.JFrame; import mcmas.core.MCMChrono; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.shapefile.ng.ShapefileDataStore; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geometry.jts.ReferencedEnvelope; import org.opengis.feature.simple.SimpleFeature; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.MultiPolygon; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Point; public class MapMain { /* public void displayLayers(File shpFile) throws IOException { System.out.println(shpFile); FileDataStore store = FileDataStoreFinder.getDataStore(shpFile); System.out.println(shpFile.length()); SimpleFeatureSource shapefileSource = store.getFeatureSource(); Style style = SLD.createPolygonStyle(Color.GREEN, null, 0.0f); final MapContent map = new MapContent(); map.setTitle("Test display GIS"); Layer shpLayer = new FeatureLayer(shapefileSource, style); map.addLayer(shpLayer); JMapFrame frame = new JMapFrame(map); frame.setSize(800, 600); frame.enableStatusBar(true); frame.enableToolBar(true); frame.setVisible(true); }*/ public void displayLayers2(URL shpFile) throws IOException { ShapefileDataStore store = new ShapefileDataStore(shpFile); String name = store.getTypeNames()[0]; SimpleFeatureSource source = store.getFeatureSource(name); SimpleFeatureCollection collection = source.getFeatures(); SimpleFeatureIterator it = collection.features(); GeometryFactory gf = new GeometryFactory(); ReferencedEnvelope env = source.getBounds(); int pict_width = (int) Math.ceil(env.getMaxX() - env.getMinX()); int pict_height = (int) Math.ceil(env.getMaxY() - env.getMinY()); System.out.println(pict_width); System.out.println(pict_height); //Point [][] points = new Point[256][256]; String [][] grid = new String[256][256]; HashMap<String, Integer> colorMap = new HashMap<String, Integer>(); Random rng = new Random(); //int index = 0; MCMChrono ch = new MCMChrono("interections").start(); /* for (int i = 0; i < points.length; i++) { for (int j = 0; j < points[i].length; j++) { final double xpos = transform(i + 0.5, 0, grid.length, env.getMinX(), env.getMaxX()); final double ypos = transform(j + 0.5, 0, grid[i].length, env.getMinY(), env.getMaxY()); points[i][j] = gf.createPoint(new Coordinate(xpos, ypos)); } }*/ while (it.hasNext()) { //index++; SimpleFeature f = it.next(); //Polygon p = new Polygon(); //System.out.println(f.getProperty("level1")); //System.out.println(f.getProperties()); MultiPolygon poly = (MultiPolygon) f.getDefaultGeometry(); //Coordinate[] c = poly.getCoordinates(); colorMap.put(f.getID(), rng.nextInt()); System.out.println(f.getID()); System.out.println(f.getAttribute("Level1")); //System.out.println(poly.getCentroid()); //System.out.println(c.length); for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { if (grid[i][j] != null) continue; final double xpos = transform(i + 0.5, 0, grid.length, env.getMinX(), env.getMaxX()); final double ypos = transform(j + 0.5, 0, grid[i].length, env.getMinY(), env.getMaxY()); //if (grid[i][j] != null) continue; //System.out.println("xpos: " + xpos); //System.out.println("ypos: " + ypos); Point point = gf.createPoint(new Coordinate(xpos, ypos)); //final Point point = points[i][j]; if (poly.contains(point)) { /*System.out.println("xpos: " + xpos); System.out.println("ypos: " + ypos); System.out.println("true");*/ grid[i][j] = f.getID(); } } } } System.out.println(ch.stop()); ch = new MCMChrono("image generation").start(); BufferedImage img = new BufferedImage(grid.length, grid.length, ColorSpace.TYPE_RGB); for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { /*if (! colorMap.containsKey(grid[i][j])) { colorMap.put(grid[i][j], rng.nextInt()); }*/ //System.out.print("" + j + " "); //System.out.println(grid.length - j); Integer c = colorMap.get(grid[i][j]); if (c != null) img.setRGB(i, grid.length - 1 - j, c); //System.out.print(grid[i][j] + " "); } //System.out.println(); } System.out.println(ch.stop()); //ImageIO.write(img, "png", new File("out.png")); System.out.println(source.getBounds()); //BufferedImage img = new BufferedImage(20000, height, imageType) System.out.println(transform(128, 0, 256, 0, 1000)); System.out.println(img); ImagePanel panel = new ImagePanel(img); JFrame frame = new JFrame("MapGIS"); frame.setContentPane(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public double transform(double n, double min1, double max1, double min2, double max2) { final double range1 = max1 - min1; final double range2 = max2 - min2; //System.out.println(range1); //System.out.println(range2); return ((n - min1) / range1) * range2 + min2; } public static void test(String[] args) throws IOException { File file = new File("110m_cultural\\110m_admin_0_countries.shp"); FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); SimpleFeatureCollection c = featureSource.getFeatures(); SimpleFeatureIterator featuresIterator = c.features(); while (featuresIterator.hasNext()) { SimpleFeature o = featuresIterator.next(); String name = (String) o.getAttribute("NAME"); Object geometry = o.getDefaultGeometry(); System.out.println(name + ", " + geometry.getClass()); } } public static void main(String[] args) throws IOException { //ClassLoader cl = ClassLoader.getSystemClassLoader(); //URL[] urls = ((URLClassLoader)cl).getURLs(); /*for(URL url: urls){ System.out.println(url.getFile()); }*/ //new MapMain().displayLayers(new File("site4.shp")); new MapMain().displayLayers2(new URL("file://data/site4.shp")); } } |