Esempio n. 1
0
 private void importActivity(JSONObject activity, String portal_image_url) {
   // System.out.println(activity.toString());
   try {
     JSONObject member = activity.getJSONObject("member");
     if (member.containsKey("id")) {
       String title =
           activity.getString("body").replace(activity.getString("image_large_url"), "").trim();
       DBObject photo = new BasicDBObject();
       photo.put("portal_image_url", portal_image_url);
       if (mPhotoCollection.count(photo) > 0) {
         System.err.println(portal_image_url + " already exists");
         return;
       } else {
         System.out.println("Inserting " + portal_image_url);
       }
       String imageSourceUrl =
           Config.PORTAL_URL.startsWith("http:")
               ? portal_image_url.replace("https://", "http://")
               : portal_image_url;
       BufferedImage[] images = ImageUtil.convertImage(new URL(imageSourceUrl));
       if (images == null) {
         System.err.println(portal_image_url + " corrupted");
         return;
       }
       saveThumbnailImages(photo, images, "/" + getFileId(portal_image_url) + ".png");
       photo.put("uploader_user_id", member.getString("id"));
       photo.put("title", title);
       Object exifObj = getExifData(portal_image_url);
       Date date = null;
       if (exifObj instanceof JSONObject) {
         JSONObject exif = (JSONObject) exifObj;
         if (exif.containsKey("date")) {
           try {
             date = FORMAT_DATE_ISO.parse(exif.getString("date"));
           } catch (ParseException e) {
             e.printStackTrace();
           }
         }
         if (exif.containsKey("location")) {
           photo.put("exif_location", exif.getJSONArray("location"));
         }
       }
       if (date == null && activity.containsKey("created_at")) {
         try {
           date = FORMAT_DATE.parse(activity.getString("created_at"));
         } catch (ParseException e) {
           e.printStackTrace();
         }
       }
       if (date != null) {
         photo.put("exif_date", date);
       }
       mPhotoCollection.insert(photo);
       // System.out.println(photo);
       // System.out.println();
     }
   } catch (JSONException | MalformedURLException e) {
     e.printStackTrace();
   }
 }
  public static List<Wall> readWalls(InputStream is) {

    int z = 0;

    List<Wall> walls = new ArrayList<>();

    try {
      JSONObject json = (JSONObject) JSON.parse(is);

      String type = json.getString("type");
      if (type.equals("FeatureCollection")) {
        JSONArray features = json.getJSONArray("features");
        Iterator<JSONObject> iter = features.iterator();
        while (iter.hasNext()) {
          JSONObject feature = iter.next();
          JSONObject properties = feature.getJSONObject("properties");
          JSONObject geometry = feature.getJSONObject("geometry");
          type = properties.getString("type");
          if (type.equals("wall")) {

            double height = Double.parseDouble(properties.getString("height"));
            double attenuation = Double.parseDouble(properties.getString("decay"));

            JSONArray coordsArray = geometry.getJSONArray("coordinates");
            int npoints = coordsArray.size();

            for (int i = 0; i < npoints - 1; i++) {
              JSONArray p0 = coordsArray.getJSONArray(i);
              JSONArray p1 = coordsArray.getJSONArray(i + 1);

              Wall wall =
                  new Wall.Builder(
                          p0.getDouble(0), p0.getDouble(1), p1.getDouble(0), p1.getDouble(1))
                      .height(height)
                      .attenuation(attenuation)
                      .build();

              walls.add(wall);
            }
          }
        }
      }
    } catch (NullPointerException | JSONException e) {
      e.printStackTrace();
    }

    return walls;
  }
  public static List<Location> readWalkLocations(InputStream is) {
    int z = 0;
    List<Location> locs = new ArrayList<>();
    try {
      JSONObject json = (JSONObject) JSON.parse(is);
      String type = json.getString("type");
      if (type.equals("FeatureCollection")) {
        JSONArray features = json.getJSONArray("features");
        Iterator<JSONObject> iter = features.iterator();
        while (iter.hasNext()) {
          JSONObject feature = iter.next();
          JSONObject properties = feature.getJSONObject("properties");
          JSONObject geometry = feature.getJSONObject("geometry");
          type = properties.getString("type");
          if (type.equals("walk")) {

            double height = Double.parseDouble(properties.getString("height"));
            int repeat = Integer.parseInt(properties.getString("repeat"));

            JSONArray coordinates = geometry.getJSONArray("coordinates");

            Iterator<JSONArray> iterCoords = coordinates.iterator();
            while (iterCoords.hasNext()) {
              JSONArray coord = iterCoords.next();

              double x = coord.getDouble(0);
              double y = coord.getDouble(1);

              Location loc = new Location(x, y, z);
              loc.setH((float) height);

              for (int t = 0; t < repeat; t++) {
                locs.add(loc);
              }
            }
          }
        }
      }
    } catch (NullPointerException | JSONException e) {
      e.printStackTrace();
    }
    return locs;
  }
 private boolean isNodeExisting(String node, JSONArray array) throws JSONException {
   for (int i = 0; i < array.length(); i++) {
     JSONObject temp = array.getJSONObject(i);
     String tempName = temp.getString("name");
     if (tempName.equalsIgnoreCase(node)) {
       return true;
     }
   }
   return false;
 }
 static JSONArray parseFeatures(JSONObject json) {
   try {
     String type = json.getString("type");
     if (type.equals("FeatureCollection")) {
       JSONArray features = json.getJSONArray("features");
       return features;
     }
   } catch (JSONException e) {
     e.printStackTrace();
   }
   return null;
 }
  public static List<BLEBeacon> readMapBLEBeacons(InputStream is) {
    int z = 0;
    List<BLEBeacon> bleBeacons = new ArrayList<>();

    try {
      JSONObject json = (JSONObject) JSON.parse(is);

      String type = json.getString("type");
      if (type.equals("FeatureCollection")) {
        JSONArray features = json.getJSONArray("features");
        Iterator<JSONObject> iter = features.iterator();
        while (iter.hasNext()) {
          JSONObject feature = iter.next();
          JSONObject properties = feature.getJSONObject("properties");
          JSONObject geometry = feature.getJSONObject("geometry");
          type = properties.getString("type");
          if (type.equals("beacon")) {

            int major = Integer.parseInt(properties.getString("major"));
            int minor = Integer.parseInt(properties.getString("minor"));
            String uuid = properties.getString("uuid");
            int outPower = Integer.parseInt(properties.getString("power"));
            int interval = Integer.parseInt(properties.getString("interval"));

            double x = geometry.getJSONArray("coordinates").getInt(0);
            double y = geometry.getJSONArray("coordinates").getInt(1);

            BLEBeacon bleBeacon = new BLEBeacon(minor, major, minor, x, y, z);
            bleBeacon.setUuid(uuid);
            bleBeacon.setOutputPower(outPower);

            bleBeacons.add(bleBeacon);
          }
        }
      }
    } catch (NullPointerException | JSONException e) {
      e.printStackTrace();
    }

    return bleBeacons;
  }
  public static List<Location> readGridLocations(InputStream is) {

    int z = 0;

    List<Location> locs = new ArrayList<>();
    try {
      JSONObject json = (JSONObject) JSON.parse(is);
      String type = json.getString("type");
      if (type.equals("FeatureCollection")) {
        JSONArray features = json.getJSONArray("features");
        Iterator<JSONObject> iter = features.iterator();
        while (iter.hasNext()) {
          JSONObject feature = iter.next();
          JSONObject properties = feature.getJSONObject("properties");
          JSONObject geometry = feature.getJSONObject("geometry");
          String propertiesType = properties.getString("type");
          String geometryType = geometry.getString("type");
          if (propertiesType.equals("grid")) {
            double height = Double.parseDouble(properties.getString("height"));
            int repeat = Integer.parseInt(properties.getString("repeat"));
            int spacing = Integer.parseInt(properties.getString("interval"));

            JSONArray coordinates = geometry.getJSONArray("coordinates");
            Iterator<JSONArray> iterCoords = null;
            if (geometryType.equals("Polygon")) {
              JSONArray exteriorRing = coordinates.getJSONArray(0);
              iterCoords = exteriorRing.iterator();
            } else if (geometryType.equals("LineString")) {
              iterCoords = coordinates.iterator();
            }

            double minx = Double.MAX_VALUE;
            double miny = Double.MAX_VALUE;
            double maxx = -Double.MAX_VALUE;
            double maxy = -Double.MAX_VALUE;
            while (iterCoords.hasNext()) {
              JSONArray coord = iterCoords.next();
              double x = coord.getDouble(0);
              double y = coord.getDouble(1);
              minx = Math.min(minx, x);
              miny = Math.min(miny, y);
              maxx = Math.max(maxx, x);
              maxy = Math.max(maxy, y);
            }

            double x = minx;
            while (x <= maxx) {
              double y = miny;
              while (y <= maxy) {
                Location loc = new Location(x, y, z);
                loc.setH((float) height);
                for (int t = 0; t < repeat; t++) {
                  locs.add(loc);
                }
                y += spacing;
              }
              x += spacing;
            }
          }
        }
      }
    } catch (NullPointerException | JSONException e) {
      e.printStackTrace();
    }
    return locs;
  }
  protected void service(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    PrintWriter out = resp.getWriter();

    String json = req.getParameter("json");
    String key = req.getParameter("key");

    String retval = new String();
    DBProc dbProc = new DBProc();

    ProfileProperties prop = dbProc.verifyProfileKey(key);

    if (prop == null) {
      out.print("Profile Key " + key + " was not Verified");
      out.flush();
      out.close();
      return;
    }

    if (!prop.ispEnabled()) {
      out.print("Profile Key " + key + " is not Enabled");
      out.flush();
      out.close();
      return;
    }

    if (!prop.isPgr_sp()) {
      out.print("ShortestPath Searches for this Profile is not enabled");
      out.flush();
      out.close();
      return;
    }

    if (!prop.ispPublic()) {
      if (!HostInfo.checkHostInfo(req.getRemoteHost(), prop.getHosts())) {
        if (!HostInfo.checkHostInfo(req.getRemoteAddr(), prop.getHosts())) {
          out.print("Not authorized to use this Service[ " + req.getRemoteAddr() + " ]");
          out.flush();
          out.close();
          return;
        }
      }
    }

    if (json == null) {
      out.print("No JSON Request Parameter");
      out.flush();
      out.close();
      return;
    }

    try {
      JSONObject jo = new JSONObject(json);
      String proj = jo.getString("projection");
      String source = jo.getJSONArray("points").getString(0);
      String target = jo.getJSONArray("points").getString(1);

      int epsg = Integer.parseInt(proj.split(":")[1]);

      if (epsg != RouteProperties.getSrid()) {
        source = dbProc.transformPoint(source, epsg);
        target = dbProc.transformPoint(target, epsg);
      }

      retval = dbProc.findShortestPath(prop.getId(), source, target, prop.isReverse_cost());
    } catch (Exception ex) {
      retval = "Error has occured";
      ex.printStackTrace();
    }

    resp.setContentType("application/json");

    out.print(retval);
    out.flush();
    out.close();
  }