Skip to content

Commit f77422a

Browse files
author
Matt
committed
adding support for GeoJson and related wrapper for showing and removing GeoJsons on the map
1 parent 7ff1fc9 commit f77422a

File tree

7 files changed

+203
-71
lines changed

7 files changed

+203
-71
lines changed

src/main/java/io/github/makbn/jlmap/exception/JLGeoJsonParserException.java

+7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
import lombok.Builder;
44

5+
/**
6+
* @author Mehdi Akbarian Rastaghi (@makbn)
7+
*/
58
public class JLGeoJsonParserException extends JLException {
69

710
@Builder
811
public JLGeoJsonParserException(String message) {
912
super(message);
1013
}
14+
15+
public JLGeoJsonParserException(Throwable cause) {
16+
super(cause);
17+
}
1118
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
package io.github.makbn.jlmap.geojson;
22

3+
import com.google.gson.JsonParseException;
34
import io.github.makbn.jlmap.exception.JLGeoJsonParserException;
4-
import lombok.Builder;
5+
import lombok.AccessLevel;
6+
import lombok.experimental.FieldDefaults;
57

6-
7-
@Builder
8-
public class JLGeoJsonContent extends JLGeoJsonSource {
9-
10-
private final String content;
11-
12-
public JLGeoJsonContent(String content) {
13-
this.content = content;
14-
}
15-
16-
@Override
17-
protected String getAddress() {
18-
return null;
19-
}
8+
/**
9+
* @author Mehdi Akbarian Rastaghi (@makbn)
10+
*/
11+
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
12+
public class JLGeoJsonContent extends JLGeoJsonSource<String> {
2013

2114
@Override
22-
protected void load() {
15+
public String load(String content) throws JLGeoJsonParserException {
2316
if (content == null || content.isEmpty())
2417
throw JLGeoJsonParserException.builder()
2518
.message("json is empty!")
2619
.build();
27-
}
28-
29-
@Override
30-
public JLGeoJsonObject getObject() {
31-
return null;
20+
try {
21+
validateJson(content);
22+
return content;
23+
} catch (JsonParseException e) {
24+
throw new JLGeoJsonParserException(e.getMessage());
25+
}
3226
}
3327
}
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
package io.github.makbn.jlmap.geojson;
22

3-
import lombok.Builder;
3+
import com.google.gson.JsonParseException;
4+
import io.github.makbn.jlmap.exception.JLGeoJsonParserException;
5+
import lombok.AccessLevel;
6+
import lombok.experimental.FieldDefaults;
47

58
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
611

7-
public class JLGeoJsonFile extends JLGeoJsonSource {
8-
9-
private final File file;
10-
11-
12-
@Builder
13-
public JLGeoJsonFile(File file) {
14-
this.file = file;
15-
}
16-
17-
@Override
18-
protected String getAddress() {
19-
return file.getAbsolutePath();
20-
}
21-
22-
@Override
23-
protected void load() {
24-
25-
}
12+
/**
13+
* @author Mehdi Akbarian Rastaghi (@makbn)
14+
*/
15+
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
16+
public class JLGeoJsonFile extends JLGeoJsonSource<File> {
2617

2718
@Override
28-
public JLGeoJsonObject getObject() {
29-
return null;
19+
public String load(File file) throws JLGeoJsonParserException {
20+
try {
21+
String fileContent = Files.readString(file.toPath());
22+
validateJson(fileContent);
23+
return fileContent;
24+
} catch (IOException | JsonParseException e) {
25+
throw new JLGeoJsonParserException(e.getMessage());
26+
}
3027
}
3128
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package io.github.makbn.jlmap.geojson;
22

3+
import lombok.AccessLevel;
4+
import lombok.Builder;
35
import lombok.Getter;
46
import lombok.Setter;
7+
import lombok.experimental.FieldDefaults;
8+
import lombok.experimental.NonFinal;
59

6-
import java.util.List;
7-
10+
/**
11+
* @author Mehdi Akbarian Rastaghi (@makbn)
12+
*/
813
@Getter
914
@Setter
15+
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
1016
public class JLGeoJsonObject {
11-
private String type;
12-
private List<JLFeature> features;
17+
@NonFinal
18+
int id;
19+
String geoJsonContent;
1320

21+
@Builder
22+
public JLGeoJsonObject(int id, String geoJsonContent) {
23+
this.id = id;
24+
this.geoJsonContent = geoJsonContent;
25+
}
1426
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
package io.github.makbn.jlmap.geojson;
22

3-
public abstract class JLGeoJsonSource {
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonSyntaxException;
5+
import io.github.makbn.jlmap.exception.JLGeoJsonParserException;
6+
import lombok.AccessLevel;
7+
import lombok.experimental.FieldDefaults;
8+
import lombok.experimental.NonFinal;
49

5-
protected abstract String getAddress();
10+
/**
11+
* The base abstract class for a GeoJSON data source. Implementations of this class are expected
12+
* to provide functionality for loading and accessing GeoJSON objects.
13+
* @author Mehdi Akbarian Rastaghi (@makbn)
14+
*
15+
* @param <S> source type
16+
*/
17+
@FieldDefaults(makeFinal = true, level = AccessLevel.PROTECTED)
18+
public abstract class JLGeoJsonSource<S> {
619

7-
protected abstract void load();
20+
/**
21+
* Gson object for JSON serialization and deserialization.
22+
*/
23+
Gson gson;
824

9-
public abstract JLGeoJsonObject getObject();
25+
/**
26+
* The GeoJSON object loaded from this source.
27+
*/
28+
@NonFinal
29+
JLGeoJsonObject geoJsonObject;
30+
31+
/**
32+
* Initializes a new instance of {@code JLGeoJsonSource} and sets up the Gson object.
33+
*/
34+
protected JLGeoJsonSource() {
35+
this.gson = new Gson();
36+
}
37+
38+
/**
39+
* Loads the GeoJSON data from the source and parses it into a GeoJSON object.
40+
*
41+
* @throws JLGeoJsonParserException If an error occurs during data loading or parsing.
42+
*/
43+
public abstract String load(S source) throws JLGeoJsonParserException;
44+
45+
protected void validateJson(String jsonInString) throws JsonSyntaxException {
46+
gson.fromJson(jsonInString, Object.class);
47+
}
1048

1149
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
package io.github.makbn.jlmap.geojson;
22

3-
import lombok.Builder;
3+
import com.google.gson.JsonParseException;
4+
import io.github.makbn.jlmap.exception.JLGeoJsonParserException;
45

6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.net.URL;
510

6-
public class JLGeoJsonURL extends JLGeoJsonSource {
7-
private final String url;
8-
9-
10-
@Builder
11-
public JLGeoJsonURL(String url) {
12-
this.url = url;
13-
}
11+
/**
12+
* @author Mehdi Akbarian Rastaghi (@makbn)
13+
*/
14+
public class JLGeoJsonURL extends JLGeoJsonSource<String> {
1415

1516
@Override
16-
protected String getAddress() {
17-
return this.url;
18-
}
17+
public String load(String url) throws JLGeoJsonParserException {
18+
try {
19+
URL jsonUrl = new URL(url);
20+
// Open a connection to the URL and create a BufferedReader to read the content
21+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(jsonUrl.openStream()))) {
22+
StringBuilder content = new StringBuilder();
23+
String line;
24+
// Read the JSON data line by line
25+
while ((line = reader.readLine()) != null) {
26+
content.append(line);
27+
}
28+
29+
validateJson(content.toString());
30+
return content.toString();
31+
32+
}
33+
} catch (IOException | JsonParseException e) {
34+
throw new JLGeoJsonParserException(e.getMessage());
35+
}
1936

20-
@Override
21-
protected void load() {
22-
23-
}
24-
25-
@Override
26-
public JLGeoJsonObject getObject() {
27-
return null;
2837
}
2938
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.github.makbn.jlmap.layer;
2+
3+
import io.github.makbn.jlmap.JLMapCallbackHandler;
4+
import io.github.makbn.jlmap.exception.JLException;
5+
import io.github.makbn.jlmap.geojson.JLGeoJsonContent;
6+
import io.github.makbn.jlmap.geojson.JLGeoJsonFile;
7+
import io.github.makbn.jlmap.geojson.JLGeoJsonObject;
8+
import io.github.makbn.jlmap.geojson.JLGeoJsonURL;
9+
import io.github.makbn.jlmap.layer.leaflet.LeafletGeoJsonLayerInt;
10+
import javafx.scene.web.WebEngine;
11+
import lombok.NonNull;
12+
import netscape.javascript.JSObject;
13+
14+
import java.io.File;
15+
import java.util.UUID;
16+
17+
/**
18+
* Represents the GeoJson (other) layer on Leaflet map.
19+
* @author Mehdi Akbarian Rastaghi (@makbn)
20+
*/
21+
public class JLGeoJsonLayer extends JLLayer implements LeafletGeoJsonLayerInt {
22+
private static final String MEMBER_PREFIX = "geoJson";
23+
private static final String WINDOW_OBJ = "window";
24+
JLGeoJsonURL fromUrl;
25+
JLGeoJsonFile fromFile;
26+
JLGeoJsonContent fromContent;
27+
JSObject window;
28+
29+
public JLGeoJsonLayer(WebEngine engine, JLMapCallbackHandler callbackHandler) {
30+
super(engine, callbackHandler);
31+
this.fromUrl = new JLGeoJsonURL();
32+
this.fromFile = new JLGeoJsonFile();
33+
this.fromContent = new JLGeoJsonContent();
34+
this.window = (JSObject) engine.executeScript(WINDOW_OBJ);
35+
}
36+
37+
@Override
38+
public JLGeoJsonObject addFromFile(@NonNull File file) throws JLException {
39+
String json = fromFile.load(file);
40+
return addGeoJson(json);
41+
}
42+
43+
@Override
44+
public JLGeoJsonObject addFromUrl(@NonNull String url) throws JLException {
45+
String json = fromUrl.load(url);
46+
return addGeoJson(json);
47+
}
48+
49+
@Override
50+
public JLGeoJsonObject addFromContent(@NonNull String content) throws JLException {
51+
String json = fromContent.load(content);
52+
return addGeoJson(json);
53+
}
54+
55+
@Override
56+
public boolean removeGeoJson(@NonNull JLGeoJsonObject object) {
57+
return Boolean.parseBoolean(engine.executeScript(String.format("removeGeoJson(%d)", object.getId())).toString());
58+
}
59+
60+
private JLGeoJsonObject addGeoJson(String jlGeoJsonObject) {
61+
try {
62+
String identifier = MEMBER_PREFIX + UUID.randomUUID();
63+
this.window.setMember(identifier, jlGeoJsonObject);
64+
String returnedId = engine.executeScript(String.format("addGeoJson(\"%s\")", identifier)).toString();
65+
66+
return JLGeoJsonObject.builder()
67+
.id(Integer.parseInt(returnedId))
68+
.geoJsonContent(jlGeoJsonObject)
69+
.build();
70+
} catch (Exception e) {
71+
throw new JLException(e);
72+
}
73+
74+
}
75+
}

0 commit comments

Comments
 (0)