Added support to fetch venues #3

Merged
adsu7578 merged 4 commits from test into develop 2022-05-23 10:26:31 +02:00
12 changed files with 1306 additions and 1 deletions

View File

@ -10,7 +10,7 @@
</parent>
<groupId>com.smhi</groupId>
<artifactId>weather</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>
<packaging>war</packaging>
<name>weather</name>
<description>Collecting current weather data in Stockholm</description>
@ -39,6 +39,13 @@
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>

View File

@ -0,0 +1,29 @@
package com.smhi.weather.controllers;
import com.smhi.weather.services.OpenVenueService;
import com.smhi.weather.voVenue.OpenVenueResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class VenueController {
final OpenVenueService openVenueService;
public VenueController(OpenVenueService openVenueService) {
this.openVenueService = openVenueService;
}
@GetMapping("/venue")
public OpenVenueResponse index() {
System.out.println("Open venue responce: ");
try {
return openVenueService.getVenueInfo();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,328 @@
package com.smhi.weather.services;
import com.fasterxml.jackson.core.*;
import com.smhi.weather.voVenue.OpenVenueResponse;
import com.smhi.weather.voVenue.VenueObject;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.LocalDate;
import java.util.*;
import static java.time.temporal.ChronoUnit.SECONDS;
@Service
public class OpenVenueService {
final String apiKey = "b8e20fd7-5654-465e-8976-35b4de902b41";
final static int NO_DAYS_BETWEEN_FETCH = 14;
private List<VenueObject> data;
private LocalDate lastFetch;
public OpenVenueService() {
// Pre load data...
try {
this.getVenueInfo();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private boolean isCacheOK(LocalDate ld) {
if (this.data == null) {
return false;
} else if (lastFetch != null && ld.plusDays(NO_DAYS_BETWEEN_FETCH).isBefore(LocalDate.now())) {
// Clear cache
this.data = null;
this.lastFetch = null;
return false;
} else {
return true;
}
}
public OpenVenueResponse getVenueInfo() throws IOException, InterruptedException {
logInfo("Start getVenueInfo");
// String tempURI = "https://openstreetgs.stockholm.se/geoservice/api/b8e20fd7-5654-465e-8976-35b4de902b41/wfs?service=wfs&version=2.0.0&request=GetFeature&typeName=Markupplatelse&CQL_FILTER=Kategorityp=%271.411I,%20Uteservering%20B-läge%27%20&outputFormat=application/json";
if (isCacheOK(this.lastFetch)) {
logInfo("Cache OK");
logInfo("Current data: " + data);
OpenVenueResponse openVenueResponse = new OpenVenueResponse();
openVenueResponse.setVenueObjects(data);
return openVenueResponse;
}
logInfo("getVenueInfo: ");
String uri = buildURI(apiKey);
logInfo("URI: " + uri);
HttpRequest request = createNewRequest(uri);
HttpResponse<String> response = handleResponse(request);
OpenVenueResponse ovr = buildResult(response.body());
// Save in cache...
this.data = ovr.getVenueObjects();
this.lastFetch = LocalDate.now();
return ovr;
// System.out.println("Responce body: " + response.body());
// ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// Root root = objectMapper.readValue(response.body(), com.smhi.weather.voVenue.Root.class);
//
// logInfo("" + response.headers());
// return buildResult(response.body());
}
private HttpResponse<String> handleResponse(HttpRequest request) throws IOException, InterruptedException {
HttpResponse<String> response = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.build()
.send(request, HttpResponse.BodyHandlers.ofString());
return response;
}
private HttpRequest createNewRequest(String uri) {
HttpRequest request = null;
try {
request = HttpRequest.newBuilder()
.uri(new URI(uri))
.timeout(Duration.of(40, SECONDS))
.GET()
.build();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return request;
}
private OpenVenueResponse buildResult(String response) {
OpenVenueResponse venueResponse = new OpenVenueResponse();
List<VenueObject> venueObjects;
venueObjects = parseJSON(response);
venueResponse.setVenueObjects(venueObjects);
return venueResponse;
}
/* for (Feature feature : root.getFeatures()) {
if (feature.getProperties().getPopulärnamn() != null) {
// double[] tempCoordinates = new double[2];
// tempCoordinates[0] = feature.getGeometry().getCoordinates().get(0);
// tempCoordinates[1] = feature.getGeometry().getCoordinates().get(1);
venueObject.setName(feature.getProperties().getPopulärnamn().toString());
// System.out.println(feature.getProperties().getPopulärnamn());
// venueObject.setCoordinates(tempCoordinates);
venueObject.setAddress(feature.getProperties().getPlats_1());
System.out.println(feature.getProperties().getPlats_1());
// venueObject.setStreetNumber(feature.getProperties().getGatunr_1().toString());
venueResponse.setVenueObject(venueObject);
}
}*/
/* private OpenResponce buildResult(Root root) {
OpenResponce responce = new OpenResponce();
DataVO dvo = new DataVO();
for (TimeSeries ts : root.timeSeries) {
for (Parameter p : ts.parameters) {
if (p.getName().equalsIgnoreCase("t")) {
dvo.setTemp(p.getValues().get(0));
}
if (p.getName().equalsIgnoreCase("Wsymb2")) {
dvo.setWsymb2(p.getValues().get(0).intValue());
responce.setDataVO(dvo);
return responce;
}
}
}
return responce;
}*/
private void logInfo(String msg) {
System.out.println(":>" + msg);
}
private String getTokenText(JsonParser parser) throws IOException {
parser.nextToken(); //next token contains value
return parser.getText(); //getting text field
}
private void skipToken(JsonParser parser, String tokenValue) throws IOException {
if (null == tokenValue) {
parser.nextToken(); // skip first..
return;
}
String[] asSkipp = {"features:["};
for (String str : asSkipp) {
if (str.equals(tokenValue)) {
parser.nextToken(); // skip..
return;
}
}
}
private boolean isBlockStart(String tv) {
return null != tv && tv.equals("type:Feature");
}
private List<VenueObject> parseJSON(String data) {
// logInfo("parseJSON:\n" + data);
List<VenueObject> dataVenueObjects = new ArrayList<>();
try {
JsonParser parser = new JsonFactory().createParser(data);
logInfo("---LOOP---");
skipToken(parser, null);
String[] asTokens = {"Kategorityp", "Plats_1", "Arkivstatus",
"VALID_TO", "Gatunr_1"};
List<String> lst = new ArrayList<String>();
boolean inBlock = false;
// starting parsing of JSON String
while (parser.nextToken() != JsonToken.END_OBJECT) {
String token = parser.getCurrentName();
String tv = token + ":" + getTokenText(parser);
// logInfo(":" + tv + ":" + inBlock );
skipToken(parser, tv);
if (true == isBlockStart(tv)) {
if (true == inBlock) {
VenueObject d = VenueObject.buildVO(lst);
// logInfo("\t->" + d);
dataVenueObjects.add(d);
lst.clear();
}
inBlock = true;
continue;
}
if ("coordinates".equals(token)) {
StringBuilder sb = new StringBuilder();
sb.append("coordinates:");
parser.nextToken();
// next token will be '[' which means JSON array
sb.append(parser.getText()).append(";");
parser.nextToken();
sb.append(parser.getText());
parser.nextToken();
parser.nextToken(); // "]"
parser.nextToken(); // "}"
// logInfo(":->" + sb.toString());
lst.add(sb.toString());
continue;
}
// Simple props...
for (String str : asTokens) {
if (str.equals(token)) {
// logInfo(":->" + tv);
lst.add(tv);
continue;
}
}
// ÅÄÖ problem: "Populärnamn", "Tillstånd_tom"
if ((token.startsWith("Popul") && token.endsWith("rnamn")) ||
(token.startsWith("Tillst") && token.endsWith("nd_tom"))) {
// logInfo(":->" + tv);
lst.add(tv);
continue;
}
if ("CHANGE_DATE".equals(token)) {
// Skip 3
for (int ii = 0; ii < 3; ++ii) {
parser.nextToken();
}
}
} // while
parser.close();
dataVenueObjects.add(VenueObject.buildVO(lst));
/*
**
*/
/*logInfo("---Loop.DataVO---" );
for (VenueObject venue : dataVenueObjects) {
logInfo("\t" + venue) ;
}
logInfo("---Loop.DataVO---" + dataVenueObjects.size());*/
} catch (Exception ex) {
ex.printStackTrace();
}
return createFilteredList(dataVenueObjects);
}
private List<VenueObject> createFilteredList(List<VenueObject> dataVenueObjects) {
Set<VenueObject> uniqueSet = new HashSet<>();
logInfo("---Loop.DataVO---");
for (VenueObject object : dataVenueObjects) {
if (object.getCategory().contains("Uteservering") &&
!Objects.equals(object.getName(), "null") &&
!object.getStatus().equalsIgnoreCase("Avslutad")) {
uniqueSet.add(object);
// logInfo("\t" + object);
}
}
List<VenueObject> filteredList = new ArrayList<>(uniqueSet);
// filteredList = fixSwedishCharacters(filteredList);
logInfo("---Loop.DataVO---" + filteredList.size());
return filteredList;
}
private List<VenueObject> fixSwedishCharacters(List<VenueObject> filteredList) {
List<VenueObject> newList = new ArrayList<>();
for (VenueObject object : filteredList) {
if (!object.getName().contains("?") && !object.getAddress().contains("?")) {
newList.add(object);
}
}
return newList;
}
private String buildURI(String apiKey) {
StringBuilder sb = new StringBuilder();
sb.append("https://openstreetgs.stockholm.se")
.append("/geoservice/api/")
.append(apiKey)
.append("/wfs?service=wfs&version=1.1.0")
.append("&request=GetFeature&typeNames=od_gis:Markupplatelse")
.append("&srsName=EPSG:4326&outputFormat=json");
return sb.toString();
}
}

View File

@ -0,0 +1,148 @@
/*
package com.smhi.weather.services;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.smhi.weather.voVenue.VenueObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ParseJson {
public ParseJson() {}
private void logInfo(String msg) {System.out.println(":>" + msg);}
private String getTokenText(JsonParser parser) throws IOException {
parser.nextToken(); //next token contains value
return parser.getText(); //getting text field
}
private void skipToken(JsonParser parser, String tv) throws IOException {
if ( null == tv ) {
parser.nextToken(); // skip first..
return;
}
String[] asSkipp = {"features:["};
for (String str : asSkipp) {
if ( str.equals(tv) ) {
parser.nextToken(); // skip..
return;
}
}
}
private boolean isBlockStart(String tv) {
return null != tv && tv.equals("type:Feature");
}
private void parseJSON(String data) {
// logInfo("parseJSON:\n" + data);
List<VenueObject> dataVenueObjects = new ArrayList<>();
try {
JsonParser parser = new JsonFactory().createParser(data);
logInfo("---LOOP---");
skipToken(parser, null);
String[] asTokens = {"Kategorityp", "Plats_1", "Arkivstatus",
"VALID_TO", "Gatunr_1"};
List<String> lst = new ArrayList<String>();
boolean inBlock = false;
// starting parsing of JSON String
while (parser.nextToken() != JsonToken.END_OBJECT) {
String token = parser.getCurrentName();
String tv = token + ":" + getTokenText(parser);
// logInfo(":" + tv + ":" + inBlock );
skipToken(parser, tv);
if ( true == isBlockStart(tv) ) {
if ( true == inBlock ) {
VenueObject d = VenueObject.buildVO(lst);
logInfo("\t->" + d);
dataVenueObjects.add(d);
lst.clear();
}
inBlock = true;
continue;
}
if ("coordinates".equals(token)) {
StringBuilder sb = new StringBuilder();
sb.append("coordinates:");
parser.nextToken();
// next token will be '[' which means JSON array
sb.append(parser.getText()).append(":");
sb.append(parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken(); // "]"
parser.nextToken(); // "}"
// logInfo(":->" + sb.toString());
lst.add(sb.toString());
continue;
}
// Simple props...
for (String str : asTokens) {
if ( str.equals(token) ) {
// logInfo(":->" + tv);
lst.add(tv);
continue;
}
}
// ÅÄÖ problem: "Populärnamn", "Tillstånd_tom"
if ( (token.startsWith("Popul") && token.endsWith("rnamn")) ||
(token.startsWith("Tillst") && token.endsWith("ånd_tom"))) {
// logInfo(":->" + tv);
lst.add(tv);
continue;
}
if ("CHANGE_DATE".equals(token)) {
// Skip 3
for ( int ii = 0; ii < 3; ++ii) {
parser.nextToken();
}
}
} // while
parser.close();
dataVenueObjects.add(VenueObject.buildVO(lst));
*/
/*
**
*//*
logInfo("---Loop.DataVO---" );
for (VenueObject venue : dataVenueObjects) {
logInfo("\t" + venue) ;
}
logInfo("---Loop.DataVO---" + dataVenueObjects.size());
} catch (Exception ex) {
ex.printStackTrace();
}
}
*/
/*
@Test
public void parseJsonStreamToPOJO() throws IOException {
// parseJSON(createStringFromFile("src/test/resources/post.json"));
// parseJSON(createStringFromFile("src/test/resources/wfs-2.json"));
parseJSON(createStringFromFile("src/test/resources/wfs-1.json"));
}*//*
}
*/

View File

@ -0,0 +1,33 @@
package com.smhi.weather.voVenue;
public class Crs{
public String type;
public Properties properties;
public Crs() {}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Crs{" +
"type='" + type + '\'' +
", properties=" + properties +
'}';
}
}

View File

@ -0,0 +1,62 @@
package com.smhi.weather.voVenue;
public class Feature{
public String type;
public String id;
public Geometry geometry;
public String geometry_name;
public Properties properties;
public Feature() {}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Geometry getGeometry() {
return geometry;
}
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
public String getGeometry_name() {
return geometry_name;
}
public void setGeometry_name(String geometry_name) {
this.geometry_name = geometry_name;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Feature{" +
"type='" + type + '\'' +
", id='" + id + '\'' +
", geometry=" + geometry +
", geometry_name='" + geometry_name + '\'' +
", properties=" + properties +
'}';
}
}

View File

@ -0,0 +1,36 @@
package com.smhi.weather.voVenue;
import java.util.ArrayList;
public class Geometry{
public String type;
public ArrayList<Double> coordinates;
public Geometry() {}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ArrayList<Double> getCoordinates() {
return coordinates;
}
public void setCoordinates(ArrayList<Double> coordinates) {
this.coordinates = coordinates;
}
@Override
public String toString() {
return "Geometry{" +
"type='" + type + '\'' +
", coordinates=" + coordinates +
'}';
}
}

View File

@ -0,0 +1,48 @@
package com.smhi.weather.voVenue;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OpenVenueResponse implements Serializable {
List<VenueObject> venueObjects = new ArrayList<>();
public List<VenueObject> getVenueObjects() {
return venueObjects;
}
public void setVenueObjects(List<VenueObject> venueObjects) {
this.venueObjects = venueObjects;
}
@Override
public String toString() {
return "OpenVenueResponse{" +
"venueObjects=" + venueObjects +
'}';
}
}
/*@JsonInclude(JsonInclude.Include.NON_NULL)
public class OpenResponce implements Serializable{
private com.smhi.weather.vo.DataVO dataVO;
public DataVO getDataVO() {
return dataVO;
}
public void setDataVO(DataVO dataVO) {
this.dataVO = dataVO;
}
@Override
public String toString() {
return "OpenResponce{" +
"dataVO=" + dataVO +
'}';
}
}*/

View File

@ -0,0 +1,342 @@
package com.smhi.weather.voVenue;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Properties{
@JsonProperty("OBJECT_ID")
public int oBJECT_ID;
@JsonProperty("VERSION_ID")
public int vERSION_ID;
@JsonProperty("FEATURE_TYPE_NAME")
public String fEATURE_TYPE_NAME;
@JsonProperty("FEATURE_TYPE_OBJECT_ID")
public int fEATURE_TYPE_OBJECT_ID;
@JsonProperty("FEATURE_TYPE_VERSION_ID")
public int fEATURE_TYPE_VERSION_ID;
@JsonProperty("MAIN_ATTRIBUTE_NAME")
public String mAIN_ATTRIBUTE_NAME;
@JsonProperty("MAIN_ATTRIBUTE_VALUE")
public String mAIN_ATTRIBUTE_VALUE;
@JsonProperty("MAIN_ATTRIBUTE_DESCRIPTION")
public String mAIN_ATTRIBUTE_DESCRIPTION;
@JsonProperty("Ärendekategori")
public String ärendekategori;
@JsonProperty("Ärendenummer")
public String ärendenummer;
@JsonProperty("Arkivstatus")
public String arkivstatus;
@JsonProperty("Populärnamn")
public Object populärnamn;
@JsonProperty("Kategorityp")
public String kategorityp;
@JsonProperty("Plats_1")
public String plats_1;
@JsonProperty("Gatunr_1")
public Object gatunr_1;
@JsonProperty("Tillstånd_from")
public String tillstånd_from;
@JsonProperty("Tillstånd_tom")
public String tillstånd_tom;
@JsonProperty("VALID_FROM")
public long vALID_FROM;
@JsonProperty("VALID_TO")
public Object vALID_TO;
@JsonProperty("CID")
public long cID;
@JsonProperty("EXTENT_NO")
public int eXTENT_NO;
@JsonProperty("EXTENT_TYPE")
public int eXTENT_TYPE;
@JsonProperty("LATERAL_POSITION")
public int lATERAL_POSITION;
@JsonProperty("LATERAL_DIST")
public double lATERAL_DIST;
@JsonProperty("POSITION")
public double pOSITION;
@JsonProperty("TRAFFIC_TYPES")
public String tRAFFIC_TYPES;
@JsonProperty("NET_ELEMENT_OBJECT_ID")
public int nET_ELEMENT_OBJECT_ID;
@JsonProperty("CREATE_DATE")
public long cREATE_DATE;
@JsonProperty("CHANGE_DATE")
public long cHANGE_DATE;
public String name;
public Properties() {}
public int getoBJECT_ID() {
return oBJECT_ID;
}
public void setoBJECT_ID(int oBJECT_ID) {
this.oBJECT_ID = oBJECT_ID;
}
public int getvERSION_ID() {
return vERSION_ID;
}
public void setvERSION_ID(int vERSION_ID) {
this.vERSION_ID = vERSION_ID;
}
public String getfEATURE_TYPE_NAME() {
return fEATURE_TYPE_NAME;
}
public void setfEATURE_TYPE_NAME(String fEATURE_TYPE_NAME) {
this.fEATURE_TYPE_NAME = fEATURE_TYPE_NAME;
}
public int getfEATURE_TYPE_OBJECT_ID() {
return fEATURE_TYPE_OBJECT_ID;
}
public void setfEATURE_TYPE_OBJECT_ID(int fEATURE_TYPE_OBJECT_ID) {
this.fEATURE_TYPE_OBJECT_ID = fEATURE_TYPE_OBJECT_ID;
}
public int getfEATURE_TYPE_VERSION_ID() {
return fEATURE_TYPE_VERSION_ID;
}
public void setfEATURE_TYPE_VERSION_ID(int fEATURE_TYPE_VERSION_ID) {
this.fEATURE_TYPE_VERSION_ID = fEATURE_TYPE_VERSION_ID;
}
public String getmAIN_ATTRIBUTE_NAME() {
return mAIN_ATTRIBUTE_NAME;
}
public void setmAIN_ATTRIBUTE_NAME(String mAIN_ATTRIBUTE_NAME) {
this.mAIN_ATTRIBUTE_NAME = mAIN_ATTRIBUTE_NAME;
}
public String getmAIN_ATTRIBUTE_VALUE() {
return mAIN_ATTRIBUTE_VALUE;
}
public void setmAIN_ATTRIBUTE_VALUE(String mAIN_ATTRIBUTE_VALUE) {
this.mAIN_ATTRIBUTE_VALUE = mAIN_ATTRIBUTE_VALUE;
}
public String getmAIN_ATTRIBUTE_DESCRIPTION() {
return mAIN_ATTRIBUTE_DESCRIPTION;
}
public void setmAIN_ATTRIBUTE_DESCRIPTION(String mAIN_ATTRIBUTE_DESCRIPTION) {
this.mAIN_ATTRIBUTE_DESCRIPTION = mAIN_ATTRIBUTE_DESCRIPTION;
}
public String getÄrendekategori() {
return ärendekategori;
}
public void setÄrendekategori(String ärendekategori) {
this.ärendekategori = ärendekategori;
}
public String getÄrendenummer() {
return ärendenummer;
}
public void setÄrendenummer(String ärendenummer) {
this.ärendenummer = ärendenummer;
}
public String getArkivstatus() {
return arkivstatus;
}
public void setArkivstatus(String arkivstatus) {
this.arkivstatus = arkivstatus;
}
public Object getPopulärnamn() {
return populärnamn;
}
public void setPopulärnamn(Object populärnamn) {
this.populärnamn = populärnamn;
}
public String getKategorityp() {
return kategorityp;
}
public void setKategorityp(String kategorityp) {
this.kategorityp = kategorityp;
}
public String getPlats_1() {
return plats_1;
}
public void setPlats_1(String plats_1) {
this.plats_1 = plats_1;
}
public Object getGatunr_1() {
return gatunr_1;
}
public void setGatunr_1(Object gatunr_1) {
this.gatunr_1 = gatunr_1;
}
public String getTillstånd_from() {
return tillstånd_from;
}
public void setTillstånd_from(String tillstånd_from) {
this.tillstånd_from = tillstånd_from;
}
public String getTillstånd_tom() {
return tillstånd_tom;
}
public void setTillstånd_tom(String tillstånd_tom) {
this.tillstånd_tom = tillstånd_tom;
}
public long getvALID_FROM() {
return vALID_FROM;
}
public void setvALID_FROM(long vALID_FROM) {
this.vALID_FROM = vALID_FROM;
}
public Object getvALID_TO() {
return vALID_TO;
}
public void setvALID_TO(Object vALID_TO) {
this.vALID_TO = vALID_TO;
}
public long getcID() {
return cID;
}
public void setcID(long cID) {
this.cID = cID;
}
public int geteXTENT_NO() {
return eXTENT_NO;
}
public void seteXTENT_NO(int eXTENT_NO) {
this.eXTENT_NO = eXTENT_NO;
}
public int geteXTENT_TYPE() {
return eXTENT_TYPE;
}
public void seteXTENT_TYPE(int eXTENT_TYPE) {
this.eXTENT_TYPE = eXTENT_TYPE;
}
public int getlATERAL_POSITION() {
return lATERAL_POSITION;
}
public void setlATERAL_POSITION(int lATERAL_POSITION) {
this.lATERAL_POSITION = lATERAL_POSITION;
}
public double getlATERAL_DIST() {
return lATERAL_DIST;
}
public void setlATERAL_DIST(double lATERAL_DIST) {
this.lATERAL_DIST = lATERAL_DIST;
}
public double getpOSITION() {
return pOSITION;
}
public void setpOSITION(double pOSITION) {
this.pOSITION = pOSITION;
}
public String gettRAFFIC_TYPES() {
return tRAFFIC_TYPES;
}
public void settRAFFIC_TYPES(String tRAFFIC_TYPES) {
this.tRAFFIC_TYPES = tRAFFIC_TYPES;
}
public int getnET_ELEMENT_OBJECT_ID() {
return nET_ELEMENT_OBJECT_ID;
}
public void setnET_ELEMENT_OBJECT_ID(int nET_ELEMENT_OBJECT_ID) {
this.nET_ELEMENT_OBJECT_ID = nET_ELEMENT_OBJECT_ID;
}
public long getcREATE_DATE() {
return cREATE_DATE;
}
public void setcREATE_DATE(long cREATE_DATE) {
this.cREATE_DATE = cREATE_DATE;
}
public long getcHANGE_DATE() {
return cHANGE_DATE;
}
public void setcHANGE_DATE(long cHANGE_DATE) {
this.cHANGE_DATE = cHANGE_DATE;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Properties{" +
"oBJECT_ID=" + oBJECT_ID +
", vERSION_ID=" + vERSION_ID +
", fEATURE_TYPE_NAME='" + fEATURE_TYPE_NAME + '\'' +
", fEATURE_TYPE_OBJECT_ID=" + fEATURE_TYPE_OBJECT_ID +
", fEATURE_TYPE_VERSION_ID=" + fEATURE_TYPE_VERSION_ID +
", mAIN_ATTRIBUTE_NAME='" + mAIN_ATTRIBUTE_NAME + '\'' +
", mAIN_ATTRIBUTE_VALUE='" + mAIN_ATTRIBUTE_VALUE + '\'' +
", mAIN_ATTRIBUTE_DESCRIPTION='" + mAIN_ATTRIBUTE_DESCRIPTION + '\'' +
", ärendekategori='" + ärendekategori + '\'' +
", ärendenummer='" + ärendenummer + '\'' +
", arkivstatus='" + arkivstatus + '\'' +
", populärnamn=" + populärnamn +
", kategorityp='" + kategorityp + '\'' +
", plats_1='" + plats_1 + '\'' +
", gatunr_1=" + gatunr_1 +
", tillstånd_from='" + tillstånd_from + '\'' +
", tillstånd_tom='" + tillstånd_tom + '\'' +
", vALID_FROM=" + vALID_FROM +
", vALID_TO=" + vALID_TO +
", cID=" + cID +
", eXTENT_NO=" + eXTENT_NO +
", eXTENT_TYPE=" + eXTENT_TYPE +
", lATERAL_POSITION=" + lATERAL_POSITION +
", lATERAL_DIST=" + lATERAL_DIST +
", pOSITION=" + pOSITION +
", tRAFFIC_TYPES='" + tRAFFIC_TYPES + '\'' +
", nET_ELEMENT_OBJECT_ID=" + nET_ELEMENT_OBJECT_ID +
", cREATE_DATE=" + cREATE_DATE +
", cHANGE_DATE=" + cHANGE_DATE +
'}';
}
}

View File

@ -0,0 +1,85 @@
package com.smhi.weather.voVenue;
import java.util.ArrayList;
import java.util.Date;
public class Root{
public String type;
public ArrayList<Feature> features;
public int totalFeatures;
public int numberMatched;
public int numberReturned;
public Date timeStamp;
public Crs crs;
public Root() {}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ArrayList<Feature> getFeatures() {
return features;
}
public void setFeatures(ArrayList<Feature> features) {
this.features = features;
}
public int getTotalFeatures() {
return totalFeatures;
}
public void setTotalFeatures(int totalFeatures) {
this.totalFeatures = totalFeatures;
}
public int getNumberMatched() {
return numberMatched;
}
public void setNumberMatched(int numberMatched) {
this.numberMatched = numberMatched;
}
public int getNumberReturned() {
return numberReturned;
}
public void setNumberReturned(int numberReturned) {
this.numberReturned = numberReturned;
}
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}
public Crs getCrs() {
return crs;
}
public void setCrs(Crs crs) {
this.crs = crs;
}
@Override
public String toString() {
return "Root{" +
"type='" + type + '\'' +
", features=" + features +
", totalFeatures=" + totalFeatures +
", numberMatched=" + numberMatched +
", numberReturned=" + numberReturned +
", timeStamp=" + timeStamp +
", crs=" + crs +
'}';
}
}

View File

@ -0,0 +1,96 @@
package com.smhi.weather.voVenue;
import java.io.Serializable;
import java.util.List;
public class VenueObject implements Serializable {
private static void logInfo(String msg) {System.out.println(":>" + msg);}
private String name;
private String address;
private String streetNo;
private String category;
private String status;
private String coordinates;
private String validTo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public String getStreetNo() {
return streetNo;
}
public String getCategory() {
return category;
}
public String getStatus() {
return status;
}
public String getCoordinates() {
return coordinates;
}
public String getValidTo() {
return validTo;
}
public static VenueObject buildVO(List<String> lst) {
VenueObject venue = new VenueObject();
for (String str : lst) {
// logInfo("\t:" + str);
String[] asStr = str.split(":");
if ( asStr[0].equals("coordinates")) {
venue.coordinates = asStr[1];
}
if ( asStr[0].equals("Arkivstatus")) {
venue.status = asStr[1];
}
if ( asStr[0].equals("Plats_1")) {
venue.address = asStr[1];
}
if ( asStr[0].startsWith("Popul") && asStr[0].endsWith("rnamn")) {
venue.name = asStr[1];
}
if ( asStr[0].equals("Kategorityp")) {
venue.category = asStr[1];
}
if ( asStr[0].equals("Gatunr_1")) {
venue.streetNo = asStr[1];
}
if ( asStr[0].equals("VALID_TO")) {
venue.validTo = asStr[1];
}
}
return venue;
}
public String toString() {
return "DataVO {" +
name + ":" +
address + ":" +
streetNo + ":" +
coordinates + ":" + "}";
}
/* @Override
public String toString() {
return "VenueObject{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", streetNo='" + streetNo + '\'' +
", coordinates='" + coordinates + '\'' +
'}';
}*/
}

View File

@ -0,0 +1,91 @@
package com.smhi.weather;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smhi.weather.voVenue.Feature;
import com.smhi.weather.voVenue.Properties;
import com.smhi.weather.voVenue.Root;
import org.json.JSONException;
import org.junit.Test;
import org.junit.Assert;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.skyscreamer.jsonassert.JSONParser.parseJSON;
public class VenueParseDataTest {
@Test
public void parseJsonToPOJO() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String path = "src/test/resources/test-b.json";
String data = createStringFromFile(path);
System.out.println("Before mapper");
Root root = mapper.readValue(data, com.smhi.weather.voVenue.Root.class);
System.out.println("After mapper");
Assert.assertNotNull(root);
/*if (root.geometry.coordinates.get(0) != null) {
Assert.assertTrue(root.geometry.coordinates.get(0) == 18.07116);
}*/
// System.out.println(data);
// System.out.println(root.toString());
System.out.println(root.getFeatures().get(0).getGeometry().getCoordinates().get(0));
// System.out.println(root.getCrs().getProperties());
// System.out.println(root);
Set<String> venues = new HashSet<>();
for (Feature feature : root.getFeatures()) {
if (feature.getProperties().getPopulärnamn() != null) {
// System.out.println(feature.getProperties().getPopulärnamn() + " : " + feature.getProperties().getArkivstatus());
venues.add(feature.getProperties().getPopulärnamn().toString());
}
}
int count = 0;
for (String venue : venues) {
count++;
System.out.println(count + ": " + venue);
}
/*for (Feature feature : root.features) {
if (feature.getProperties().getTillstånd_tom().contains("202")) {
System.out.println(feature.getProperties().getTillstånd_tom());
if (feature.getProperties().getPopulärnamn() != null) {
System.out.println(feature.getProperties().getPopulärnamn() + " : " + feature.getProperties().getArkivstatus());
}
}
*//*if (feature.getProperties().arkivstatus.equalsIgnoreCase("pågående")) {
System.out.println(feature.getProperties().getPopulärnamn() + " : " + feature.getProperties().getArkivstatus());
}*//*
}*/
}
private String createStringFromFile(String path) throws IOException {
String result;
result = new String(Files.readAllBytes(Paths.get(path)));
return result;
}
/* @Test
public void parseJsonStreamToPOJO() throws IOException, JSONException {
ParseJson parser = new ParseJson();
// parseJSON(createStringFromFile("src/test/resources/post.json"));
// parseJSON(createStringFromFile("src/test/resources/wfs-2.json"));
parser.parseJSON(createStringFromFile("src/test/resources/wfs.json"));
}*/
}