-3

I am making a program to scrape the value of Elevation from a web page. The webpage address is - https://nationalmap.gov/epqs/pqs.php?x=35.227085&y=-80.843123&units=Meters&output=json

When we open the above link, the webpage displayed is -

{"USGS_Elevation_Point_Query_Service":{"Elevation_Query"{"x":35.227085,"y":-80.843123,"Data_Source":"3DEP 1/3 arc-second","Elevation":"-1000000","Units":"Meters"}}}

I am not able to print the value of only Elevation i.e, -1000000 from the data displayed on the webpage. I am required to do it using JAVA. Please help. I am new to programming.

I tried a code below. But is prints entire content of webpage. I require only elevation value.

URL url;
    InputStream is = null;
    BufferedReader br;
  

    try {
        url = new URL("https://nationalmap.gov/epqs/pqs.php?x=35.227085&y=-80.843124&units=Meters&output=json");
        is = url.openStream();  // throws an IOException
        br = new BufferedReader(new InputStreamReader(is));

     //  System.out.println (br.lines());
        while ((line = br.readLine()) != null) {
            System.out.println(line);    
        }
        

I want only the elevation value using JAVA.

Manu
  • 177
  • 9
  • you are new in Java, yet you attempt to perform web scraping using Java? – Stultuske Jan 17 '19 at 07:48
  • It is related to my project work. I have to find elevation values for a large number of latitudes and longitudes and do some kind of calculations on these elevation values. – Manu Jan 17 '19 at 07:57
  • I did a lot of searching on JSON. I couldnot solve this problem. I tried to convert the string line into character array and then print the particular value of elevation, but it gave me null exception error. – Manu Jan 17 '19 at 07:59
  • Possible duplicate of [Convert InputStream into JSON](https://stackoverflow.com/questions/18794427/convert-inputstream-into-json) – Oleg Cherednik Jan 17 '19 at 08:33

1 Answers1

1

This URL contains json data, so you need to use any json framework, e.g. Jackson.

First of all, you have to define json model of the returned message:

public static class Model {
    @JsonProperty("USGS_Elevation_Point_Query_Service")
    private ElevationPointQuery elevationPointQuery;

    public static class ElevationPointQuery {
        @JsonProperty("Elevation_Query")
        private Data data;

        public static class Data {
            @JsonProperty
            private double x;
            @JsonProperty
            private double y;
            @JsonProperty("Data_Source")
            private String dataSource;
            @JsonProperty("Elevation")
            private String elevation;
            @JsonProperty("Units")
            private String units;
        }

    }
}

Then you have to define method, that reads json message from given url and returns the model:

private static <T> T readJsonByUrl(URL url, Class<T> cls) throws IOException {
    try (InputStream in = url.openStream()) {
        return new ObjectMapper().readerFor(cls).readValue(in);
    }
}

Finally, you are ready to read the model and get required data:

final URL url = new URL("https://nationalmap.gov/epqs/pqs.php?x=35.227085&y=-80.843124&units=Meters&output=json");
Model data = readJsonByUrl(url, Model.class);
System.out.println("Elevation = " + data.elevationPointQuery.data.elevation);

To use Jackson, you have to add following dependencies:

compile 'com.fasterxml.jackson.core:jackson-core:2.9.8'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.8'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35