Пример #1
0
  private static FeatureSource openShapefile(final String sFile) throws IOException {

    final FileDataStore store = FileDataStoreFinder.getDataStore(new File(sFile));
    final FeatureSource featureSource = store.getFeatureSource();

    return featureSource;
  }
  private static void constructMapUI(final File shapefile) {

    JMapFrame frame;
    MapContent map = new MapContent();

    FileDataStore dataStore;
    SimpleFeatureSource shapefileSource;

    try {
      dataStore = FileDataStoreFinder.getDataStore(shapefile);
      shapefileSource = dataStore.getFeatureSource();
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }
    Style shpStyle = SLD.createPolygonStyle(Color.RED, null, 0.0f);
    Layer shpLayer = new FeatureLayer(shapefileSource, shpStyle);
    map.addLayer(shpLayer);

    frame = new JMapFrame(map);
    frame.enableLayerTable(true);
    frame.setSize(1000, 800);
    frame.enableStatusBar(true);
    frame.enableToolBar(true);
    frame.setTitle("Map Viewer (courtesy of GeoTools");

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    frame.setVisible(true);

    frame.setDefaultCloseOperation(JMapFrame.HIDE_ON_CLOSE);
  }
Пример #3
0
  /**
   * This method connects to the shapefile; retrieves information about its features; creates a map
   * frame to display the shapefile and adds a custom feature selection tool to the toolbar of the
   * map frame.
   */
  public void displayShapefile(File file) throws Exception {
    FileDataStore store = FileDataStoreFinder.getDataStore(file);
    featureSource = store.getFeatureSource();
    setGeometry();

    /*
     * Create the JMapFrame and set it to display the shapefile's features
     * with a default line and colour style
     */
    MapContext map = new DefaultMapContext();
    map.setTitle("Feature selection tool example");
    Style style = createDefaultStyle();
    map.addLayer(featureSource, style);
    mapFrame = new JMapFrame(map);
    mapFrame.enableToolBar(true);
    mapFrame.enableStatusBar(true);

    /*
     * Before making the map frame visible we add a new button to its
     * toolbar for our custom feature selection tool
     */
    JToolBar toolBar = mapFrame.getToolBar();
    JButton btn = new JButton("Select");
    toolBar.addSeparator();
    toolBar.add(btn);

    /*
     * When the user clicks the button we want to enable
     * our custom feature selection tool. Since the only
     * mouse action we are intersted in is 'clicked', and
     * we are not creating control icons or cursors here,
     * we can just create our tool as an anonymous sub-class
     * of CursorTool.
     */
    btn.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent e) {
            mapFrame
                .getMapPane()
                .setCursorTool(
                    new CursorTool() {

                      @Override
                      public void onMouseClicked(MapMouseEvent ev) {
                        selectFeatures(ev);
                      }
                    });
          }
        });

    /** Finally, we display the map frame. When it is closed this application will exit. */
    mapFrame.setSize(600, 600);
    mapFrame.setVisible(true);
  }
Пример #4
0
  public static List<SimpleFeature> fromShapefile(String shapePath) throws IOException {

    FileDataStore store = FileDataStoreFinder.getDataStore(new File(shapePath));
    FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = store.getFeatureSource();
    FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection =
        featureSource.getFeatures();

    List<SimpleFeature> featuresList = new ArrayList<SimpleFeature>();
    FeatureIterator<SimpleFeature> featureIterator = featureCollection.features();
    while (featureIterator.hasNext()) {
      SimpleFeature feature = featureIterator.next();
      featuresList.add(feature);
    }
    featureIterator.close();

    return featuresList;
  }
Пример #5
0
  public static void printID(String filename) {
    File file = new File(filename);
    FileDataStore store = null;
    try {
      store = FileDataStoreFinder.getDataStore(file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureSource featureSource = null;
    try {
      featureSource = store.getFeatureSource();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureCollection featureCollection = null;
    try {
      featureCollection = featureSource.getFeatures();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // get features from source
    int sizeFeatureCollection = featureCollection.size();
    System.out.println(
        "The number of features in the shapefile is: " + sizeFeatureCollection + ".");

    SimpleFeatureIterator iterator = featureCollection.features();

    //		List<Geometry> GeometryList = new ArrayList<Geometry>(sizeFeatureCollection);
    try {
      while (iterator.hasNext()) {

        SimpleFeature feature = iterator.next();

        System.out.println(feature.getID());
      }
    } finally {
      iterator.close();

      store.dispose();
    }
  }
Пример #6
0
  /** The constructor. */
  public ViewTest() {
    // Create a map content and add our shapefile to it
    map = new MapContent();
    map.setTitle("simple map content");

    // hey, try for an image aswell
    String path = "/Users/ian/Desktop/ukrasterchart/2_BRITISH_ISLES.tif";
    File chartFile = new File(path);
    if (!chartFile.exists()) System.err.println("CANNOT FILE THE CHART FILE!!!");

    WorldImageFormat format = new WorldImageFormat();
    AbstractGridCoverage2DReader tiffReader = format.getReader(chartFile);
    if (tiffReader != null) {
      StyleFactoryImpl sf = new StyleFactoryImpl();
      RasterSymbolizer symbolizer = sf.getDefaultRasterSymbolizer();
      Style defaultStyle = SLD.wrapSymbolizers(symbolizer);

      GeneralParameterValue[] params = null;

      GridReaderLayer res = new GridReaderLayer(tiffReader, defaultStyle, params);
      map.addLayer(res);
    }

    try {
      URL url = GtActivator.getDefault().getBundle().getEntry("data/50m_admin_0_countries.shp");
      String filePath = FileLocator.resolve(url).getFile();
      File file = new File(filePath);
      if (!file.exists()) System.err.println("can't find file!!!");
      FileDataStore store = FileDataStoreFinder.getDataStore(file);
      if (store != null) {
        SimpleFeatureSource featureSource = store.getFeatureSource();

        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
        map.addLayer(layer);
      }

    } catch (IOException e) {

    }
    //
  }
Пример #7
0
  public static void getAIDBufferByID(String filename, int bufferSize) {

    File file = new File(filename);
    String[] splited = filename.split("[\\\\/]");
    String shapefilename = splited[splited.length - 1].split("\\.")[0] + ".";
    FileDataStore store = null;
    try {
      store = FileDataStoreFinder.getDataStore(file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureSource featureSource = null;
    try {
      featureSource = store.getFeatureSource();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureCollection featureCollection = null;
    try {
      featureCollection = featureSource.getFeatures();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // get features from source
    int sizeFeatureCollection = featureCollection.size();
    System.out.println(
        "The number of features in the shapefile is: " + sizeFeatureCollection + ".");

    int sum = 0;

    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints());

    int numBuffs = sizeFeatureCollection / bufferSize;
    int buff = 0;
    List<SimpleFeatureCollection> buffers = new ArrayList<SimpleFeatureCollection>();
    for (int i = 0; i < numBuffs; i++) {
      int buffB = buff;
      buff += bufferSize * (i + 1);
      Set<FeatureId> fids = new HashSet<FeatureId>();
      for (int id = buffB + 1; id < buff + 1; id++) {
        fids.add(ff.featureId(shapefilename + id));
      }
      Filter filter = ff.id(fids);
      SimpleFeatureCollection featuresF1 = null;
      try {
        featuresF1 = featureSource.getFeatures(filter);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      buffers.add(featuresF1);
    }
    Set<FeatureId> fids = new HashSet<FeatureId>();
    for (int id = buff + 1; id < sizeFeatureCollection + 1; id++) {
      fids.add(ff.featureId(shapefilename + id));
    }
    Filter filter = ff.id(fids);
    SimpleFeatureCollection featuresF1 = null;
    try {
      featuresF1 = featureSource.getFeatures(filter);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    buffers.add(featuresF1);
    // (i,j) pairs of buffers
    for (int i = 0; i < numBuffs + 1; i++) {
      System.out.println("B1: " + i);
      List<double[]> temp1List = new ArrayList<double[]>();
      SimpleFeatureCollection features1 = buffers.get(i);
      try (SimpleFeatureIterator iterator = features1.features()) {
        while (iterator.hasNext()) {
          BoundingBox a1b = iterator.next().getBounds();
          double[] a1m = new double[4];
          a1m[0] = a1b.getMinX();
          a1m[1] = a1b.getMaxX();
          a1m[2] = a1b.getMinY();
          a1m[3] = a1b.getMaxY();
          temp1List.add(a1m);
        }
      }
      int numF1 = temp1List.size();
      // j=i
      System.out.println("B2: " + i);
      for (int id1 = 0; id1 < numF1; id1++) {
        double[] a1m = temp1List.get(id1);
        for (int id2 = id1 + 1; id2 < numF1; id2++) {
          double[] a2m = temp1List.get(id2);
          if (GeometryRelated.rectangleIntersect(a1m, a2m)) {
            sum += 2;
          }
        }
      }

      // j>i
      for (int j = i + 1; j < numBuffs + 1; j++) {
        System.out.println("B2: " + j);
        List<double[]> temp2List = new ArrayList<double[]>();
        SimpleFeatureCollection features2 = buffers.get(j);
        try (SimpleFeatureIterator iterator = features2.features()) {
          while (iterator.hasNext()) {
            BoundingBox a2b = iterator.next().getBounds();
            double[] a2m = new double[4];
            a2m[0] = a2b.getMinX();
            a2m[1] = a2b.getMaxX();
            a2m[2] = a2b.getMinY();
            a2m[3] = a2b.getMaxY();
            temp2List.add(a2m);
          }
        }
        int numF2 = temp2List.size();
        for (int id1 = 0; id1 < numF1; id1++) {
          double[] a1m = temp1List.get(id1);
          for (int id2 = 0; id2 < numF2; id2++) {
            double[] a2m = temp2List.get(id2);
            if (GeometryRelated.rectangleIntersect(a1m, a2m)) {
              sum += 2;
            }
          }
        }
      }
    }

    store.dispose();

    System.out.println("Total MBR Intersection#:" + sum);
    System.out.println(
        "Average Intersection Degree: " + ((double) sum / ((double) sizeFeatureCollection)));
  }
Пример #8
0
  public static void getAIDBuffer(String filename, int bufferSize) {

    File file = new File(filename);
    FileDataStore store = null;
    try {
      store = FileDataStoreFinder.getDataStore(file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureSource featureSource = null;
    try {
      featureSource = store.getFeatureSource();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureCollection featureCollection = null;
    try {
      featureCollection = featureSource.getFeatures();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // get features from source
    int sizeFeatureCollection = featureCollection.size();
    System.out.println(
        "The number of features in the shapefile is: " + sizeFeatureCollection + ".");

    int sum = 0;
    int buf = 0;
    List<double[]> temp1GeometryList = new ArrayList<double[]>();

    try (SimpleFeatureIterator iterator = featureCollection.features()) {
      while (iterator.hasNext()) {
        buf++;
        BoundingBox a1b = iterator.next().getBounds();
        double[] a1m = new double[4];
        a1m[0] = a1b.getMinX();
        a1m[1] = a1b.getMaxX();
        a1m[2] = a1b.getMinY();
        a1m[3] = a1b.getMaxY();
        temp1GeometryList.add(a1m);
        if (buf % bufferSize == 0) {
          System.out.println("Buffer 1.");
          try (SimpleFeatureIterator iterator2 = featureCollection.features()) {
            List<double[]> temp2GeometryList = new ArrayList<double[]>();
            int buf2 = 0;
            while (iterator2.hasNext()) {
              buf2++;
              BoundingBox a2b = iterator2.next().getBounds();
              double[] a2m = new double[4];
              a2m[0] = a2b.getMinX();
              a2m[1] = a2b.getMaxX();
              a2m[2] = a2b.getMinY();
              a2m[3] = a2b.getMaxY();
              temp2GeometryList.add(a2m);
              if (buf2 % bufferSize == 0) {
                System.out.println("Buffer 2.");
                for (double[] a11m : temp1GeometryList) {
                  for (double[] a22m : temp2GeometryList) {
                    if (GeometryRelated.rectangleIntersect(a11m, a22m)) {
                      sum += 1;
                    }
                  }
                }
                temp2GeometryList = new ArrayList<double[]>();
              }
            }
            System.out.println("Buffer 2 left.");
            for (double[] a11m : temp1GeometryList) {
              for (double[] a22m : temp2GeometryList) {
                if (GeometryRelated.rectangleIntersect(a11m, a22m)) {
                  sum += 1;
                }
              }
            }
            temp1GeometryList = new ArrayList<double[]>();
          }
        }
      }
      System.out.println("Buffer 1 left.");
      try (SimpleFeatureIterator iterator2 = featureCollection.features()) {
        List<double[]> temp2GeometryList = new ArrayList<double[]>();

        int buf2 = 0;
        while (iterator2.hasNext()) {
          buf2++;
          BoundingBox a2b = iterator2.next().getBounds();
          double[] a2m = new double[4];
          a2m[0] = a2b.getMinX();
          a2m[1] = a2b.getMaxX();
          a2m[2] = a2b.getMinY();
          a2m[3] = a2b.getMaxY();
          temp2GeometryList.add(a2m);

          if (buf2 % bufferSize == 0) {
            System.out.println("Buffer 2.");
            for (double[] a11m : temp1GeometryList) {
              for (double[] a22m : temp2GeometryList) {
                if (GeometryRelated.rectangleIntersect(a11m, a22m)) {
                  sum += 1;
                }
              }
            }
            temp2GeometryList = new ArrayList<double[]>();
          }
        }
        System.out.println("Buffer 2 left.");
        for (double[] a11m : temp1GeometryList) {
          for (double[] a22m : temp2GeometryList) {
            if (GeometryRelated.rectangleIntersect(a11m, a22m)) {
              sum += 1;
            }
          }
        }
      }
    }

    store.dispose();

    sum -= sizeFeatureCollection;
    System.out.println("Total MBR Intersection#:" + sum);
    System.out.println(
        "Average Intersection Degree: " + ((double) sum / ((double) sizeFeatureCollection)));
  }
Пример #9
0
  // Not efficient: for each a1, a2 will iterate over the whole dataset
  public static void getAIDSemiBuffer(String filename) {

    File file = new File(filename);
    FileDataStore store = null;
    try {
      store = FileDataStoreFinder.getDataStore(file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureSource featureSource = null;
    try {
      featureSource = store.getFeatureSource();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureCollection featureCollection = null;
    try {
      featureCollection = featureSource.getFeatures();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // get features from source
    int sizeFeatureCollection = featureCollection.size();
    System.out.println(
        "The number of features in the shapefile is: " + sizeFeatureCollection + ".");

    int sum = 0;
    int it1 = 0;
    int buf1 = 0;
    List<double[]> tempGeometryList1 = new ArrayList<double[]>();
    //		List<Geometry> GeometryList = new ArrayList<Geometry>(sizeFeatureCollection);
    try (SimpleFeatureIterator iterator = featureCollection.features()) {
      while (iterator.hasNext()) {

        buf1++;
        SimpleFeature feature = iterator.next();
        BoundingBox a1b = feature.getBounds();
        double[] a1m = new double[4];
        a1m[0] = a1b.getMinX();
        a1m[1] = a1b.getMaxX();
        a1m[2] = a1b.getMinY();
        a1m[3] = a1b.getMaxY();
        tempGeometryList1.add(a1m);
        if (buf1 % 10000 == 0) {
          for (double[] a1m1 : tempGeometryList1) {
            it1++;
            // TODO: don't get g2 for each a11 in g1, but share it!
            try (SimpleFeatureIterator iterator2 = featureCollection.features()) {
              int it2 = 0;
              int buf2 = 0;
              List<double[]> tempGeometryList2 = new ArrayList<double[]>();
              while (iterator2.hasNext()) {
                it2++;
                SimpleFeature feature2 = iterator2.next();
                //							String a2ID = feature2.getID();
                if (it1 != it2) {
                  buf2++;
                  BoundingBox a2b = feature2.getBounds();
                  double[] a2m = new double[4];
                  a2m[0] = a2b.getMinX();
                  a2m[1] = a2b.getMaxX();
                  a2m[2] = a2b.getMinY();
                  a2m[3] = a2b.getMaxY();
                  tempGeometryList2.add(a2m);
                }
                if (buf2 % 10000 == 0) {
                  for (double[] a2m1 : tempGeometryList2) {
                    if (GeometryRelated.rectangleIntersect(a1m1, a2m1)) {
                      sum += 1;
                    }
                  }
                  tempGeometryList2 = new ArrayList<double[]>();
                }
              }
              for (double[] a2m1 : tempGeometryList2) {
                if (GeometryRelated.rectangleIntersect(a1m1, a2m1)) {
                  sum += 1;
                }
              }
            }
          }
          tempGeometryList1 = new ArrayList<double[]>();
        }
      }
      for (double[] a1m1 : tempGeometryList1) {
        it1++;
        try (SimpleFeatureIterator iterator2 = featureCollection.features()) {
          int it2 = 0;
          int buf2 = 0;
          List<double[]> tempGeometryList2 = new ArrayList<double[]>();
          while (iterator2.hasNext()) {
            it2++;
            SimpleFeature feature2 = iterator2.next();
            //							String a2ID = feature2.getID();
            if (it1 != it2) {
              buf2++;
              BoundingBox a2b = feature2.getBounds();
              double[] a2m = new double[4];
              a2m[0] = a2b.getMinX();
              a2m[1] = a2b.getMaxX();
              a2m[2] = a2b.getMinY();
              a2m[3] = a2b.getMaxY();
              tempGeometryList2.add(a2m);
            }
            if (buf2 % 10000 == 0) {
              for (double[] a2m1 : tempGeometryList2) {
                if (GeometryRelated.rectangleIntersect(a1m1, a2m1)) {
                  sum += 1;
                }
              }
              tempGeometryList2 = new ArrayList<double[]>();
            }
          }
          for (double[] a2m1 : tempGeometryList2) {
            if (GeometryRelated.rectangleIntersect(a1m1, a2m1)) {
              sum += 1;
            }
          }
        }
      }

    } finally {
      store.dispose();
    }
    System.out.println("Total MBR Intersection:" + sum);
    System.out.println(
        "Average Intersection Degree: " + ((double) sum / ((double) sizeFeatureCollection)));
  }
Пример #10
0
  public static void getAIDNoBuffer(String filename) {

    File file = new File(filename);
    FileDataStore store = null;
    try {
      store = FileDataStoreFinder.getDataStore(file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureSource featureSource = null;
    try {
      featureSource = store.getFeatureSource();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureCollection featureCollection = null;
    try {
      featureCollection = featureSource.getFeatures();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // get features from source
    int sizeFeatureCollection = featureCollection.size();
    System.out.println(
        "The number of features in the shapefile is: " + sizeFeatureCollection + ".");

    int sum = 0;
    //		List<Geometry> GeometryList = new ArrayList<Geometry>(sizeFeatureCollection);
    try (SimpleFeatureIterator iterator = featureCollection.features()) {
      while (iterator.hasNext()) {
        try (SimpleFeatureIterator iterator2 = featureCollection.features()) {
          SimpleFeature feature = iterator.next();
          String a1ID = feature.getID();
          //				System.out.println(a1ID);
          BoundingBox a1b = feature.getBounds();
          double[] a1m = new double[4];
          a1m[0] = a1b.getMinX();
          a1m[1] = a1b.getMaxX();
          a1m[2] = a1b.getMinY();
          a1m[3] = a1b.getMaxY();

          // original code that is not chunk
          while (iterator2.hasNext()) {
            SimpleFeature feature2 = iterator2.next();
            String a2ID = feature2.getID();
            if (!a1ID.equals(a2ID)) {
              BoundingBox a2b = feature2.getBounds();
              double[] a2m = new double[4];
              a2m[0] = a2b.getMinX();
              a2m[1] = a2b.getMaxX();
              a2m[2] = a2b.getMinY();
              a2m[3] = a2b.getMaxY();
              if (GeometryRelated.rectangleIntersect(a1m, a2m)) {
                sum += 1;
              }
            }
          }
        }
      }
    }

    store.dispose();

    System.out.println("Total MBR Intersection:" + sum);
    System.out.println(
        "Average Intersection Degree: " + ((double) sum / ((double) sizeFeatureCollection)));
  }
Пример #11
0
  public static void getAIDMemBound(String filename) {
    File file = new File(filename);
    FileDataStore store = null;
    try {
      store = FileDataStoreFinder.getDataStore(file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureSource featureSource = null;
    try {
      featureSource = store.getFeatureSource();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    SimpleFeatureCollection featureCollection = null;
    try {
      featureCollection = featureSource.getFeatures();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } // get features from source
    int sizeFeatureCollection = featureCollection.size();
    System.out.println(
        "The number of features in the shapefile is: " + sizeFeatureCollection + ".");

    int sum = 0;
    List<double[]> mbrList = new ArrayList<double[]>(sizeFeatureCollection);
    try (SimpleFeatureIterator iterator = featureCollection.features()) {
      while (iterator.hasNext()) {
        BoundingBox a1b = iterator.next().getBounds();
        double[] a1m = new double[4];
        a1m[0] = a1b.getMinX();
        a1m[1] = a1b.getMaxX();
        a1m[2] = a1b.getMinY();
        a1m[3] = a1b.getMaxY();
        mbrList.add(a1m);
      }
    }
    for (int i = 0; i < sizeFeatureCollection; i++) {
      int inter = 0;
      double[] a1m = mbrList.get(i);
      for (int j = i + 1; j < sizeFeatureCollection; j++) {
        double[] a2m = mbrList.get(j);
        if (GeometryRelated.rectangleIntersect(a1m, a2m)) {
          sum += 2;
          inter++;
        }
      }
      if (inter > 200) {
        System.out.println(inter + "," + i);
      }
    }

    store.dispose();
    //		sum -= sizeFeatureCollection;
    System.out.println("Total MBR Intersection:" + sum);
    System.out.println(
        "Average Intersection Degree: " + ((double) sum / ((double) sizeFeatureCollection)));
  }