SimpleGraph-Tutorial/Geo

From BITPlan Wiki
Jump to navigation Jump to search
OsProject
id  com.bitplan.simplegraph-tutorial-geo
state  
owner  BITPlan
title  SimpleGraph Geo Tutorial
url  https://github.com/BITPlan/com.bitplan.simplegraph-tutorial-geo
version  0.0.1
description  
date  
since  
until  

SimpleGraph Geo Showcase

Openly available Geographic data from different APIs seems to be a good basis for showcasing the SimpleGraph approach.

Charging Stations example

We'd like to gather data on Charging stations from different sources:

  1. Bundesnetzagentur (Excel File)
  2. Openstreetmap (API)
  3. OpenchargeMap (API)

First step: Loading data from Bundesnetzagentur

The ChargingStation Map of Bundesnetzagentur is based on the Date from the Excel file Download

Let's read it with our SimpleGraph-Excel Module.

package com.bitplan.simplegraph.geotutorial;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.Test;

import com.bitplan.simplegraph.core.SimpleNode;
import com.bitplan.simplegraph.excel.ExcelSystem;

public class TestChargingStations {

  boolean debug = true;

  /**
   * test reading the list of registered german charging stations from
   * Bundesnetzagentur
   * 
   * @throws Exception
   */
  @Test
  public void testBundesnetzagentur() throws Exception {
    // The original file has some superfluous sheets and the title row is not
    // in the first line so we downloaded and adapted it a bit to avoid to do this in software e.g.
    // as outlined in https://stackoverflow.com/questions/1834971/removing-a-row-from-an-excel-sheet-with-apache-poi-hssf
    // String url = "https://www.bundesnetzagentur.de/SharedDocs/Downloads/DE/Sachgebiete/Energie/Unternehmen_Institutionen/HandelundVertrieb/Ladesaeulen/Ladesaeulenkarte_Datenbankauszug20.xlsx?__blob=publicationFile&v=2";
    File excelFile = new File(
        "src/test/data/Bundesnetzagentur/Ladesaeulenkarte_Datenbankauszug20.xlsx");
    ExcelSystem es = new ExcelSystem();
    es.connect();
    es.moveTo(excelFile.toURI().toString());
    long count = es.g().V().count().next().longValue();
    assertEquals(7733, count);
    if (debug)
      SimpleNode.dumpGraph(es.graph());
      //es.g().V().has("sheetname").out("rows").forEachRemaining(v -> {
      //  SimpleNode.printDebug.accept(v);
      // });
  }

}

Second step: check data agains OpenChargeMap API

Let's check the result comparing to the entries in the OpenchargeMap API.

http://api.openchargemap.io/v2/poi/?output=json&latitude=50.598&longitude=7.434&maxresults=10
 @Test
  public void testOpenChargeMapApi() throws Exception {
    ExcelSystem es=this.getBundesnetzAgenturChargingStations();
    // number of stations to check
    int limit=20;
    // number of stations nearby to get from open chargemap api
    int maxresults = 3;
    es.g().V().has("row").limit(limit).forEachRemaining(v -> {
      if (debug)
        SimpleNode.printDebug.accept(v);
      String address = v.property("Adresse").value().toString();
      String ziploc = v.property("Postleitzahl Ort").value().toString();
      double lat = Double
          .parseDouble(v.property("Breitengrad [DG]").value().toString());
      double lon = Double
          .parseDouble(v.property("Längengrad [DG]").value().toString());
      System.out.println(
          String.format("%30s %30s %.4f %.4f", ziploc, address, lat, lon));
     
      String apiUrl = String.format(Locale.ENGLISH,
          "http://api.openchargemap.io/v2/poi/?output=json&latitude=%.4f8&longitude=%.4f&maxresults=10",
          lat, lon, maxresults);
      JsonSystem js = new JsonSystem();
      try {
        js.connect();
      } catch (Exception e) {
        fail(e.getMessage());
      }
      js.moveTo(apiUrl);

      GraphTraversal<Vertex, Vertex> addressesByDistance = js.g().V()
          .hasLabel("AddressInfo").order().by("Distance");
      assertTrue(addressesByDistance.hasNext());
      Vertex av = addressesByDistance.next();
      double maxDist = 1.0;
      Number adistance = (Number) av.property("Distance").value();
      Number alat = (Number) av.property("Latitude").value();
      Number alon = (Number) av.property("Longitude").value();
      // assertTrue(adistance.doubleValue()<maxDist);
      String azip = av.property("Postcode").value().toString();
      String acity = av.property("Town").value().toString();
      String aadr = av.property("AddressLine1").value().toString();

      System.out.println(String.format("%30s %30s %.4f %.4f: %.3f km",
          azip + " " + acity, aadr, alat.doubleValue(), alon.doubleValue(),
          adistance.doubleValue()));

      if (debug) {
        SimpleNode.printDebug.accept(av);
        SimpleNode.dumpGraph(js.graph());
      }

    });

  }