1

I am getting GeoJSON data from postgresql, but it seems to be invalid. How can I validate it?

After getting GeoJSON format data I want to put on map for projection. I am using LeafLet, java EE, postgresql.

<%@page import="java.io.Console"%>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="map"></div>
<%
JSONObject GJson = (JSONObject) request.getAttribute("geoJson");
//String GJson = (String)request.getAttribute("geoJson");
System.out.println("data at jsp : "+GJson);
%> 
<script type="text/javascript">
var geojson = <%= GJson%>
console.log(JSON.stringify(geojson));
var map = L.map('map');
//var geoJsonData = GeoJSON.parse(json);
var jsonLayer = L.geoJson().addTo(map);
console.log(map.hasLayer(jsonLayer));
jsonLayer.addData(geojson);
</script> 
</body>
</html>

GeoJSON retriving code(edited):

<div id="map"></div>
<%
//JSONObject GJson = (JSONObject) request.getAttribute("geoJson");
ArrayList GJson = (ArrayList)request.getAttribute("geoJson");
//String GJson = (String) request.getAttribute("geoJson");
//String GJson = (String)request.getAttribute("geoJson");
System.out.println("data at jsp : "+GJson);
%>
<script type="text/javascript">
var geojson = <%= GJson%>
var temp = "{\"type\":\"Feature\",\"properties\":{},\"geometry\":"+JSON.stringify(geojson)+"}";
console.log(temp);
var map = L.map('map');
//var geoJsonData = GeoJSON.parse(json);
var jsonLayer = L.geoJson(temp).addTo(map);
console.log(map.hasLayer(jsonLayer));
</script>
5
  • If you just need to check whether it's valid and see where the errors are, GeoJSONLint and geojson.io will do that for you. If you post a sample of the GeoJSON here, people may also be able to help diagnose the problem. Apr 6, 2016 at 6:14
  • yes have tried this, GEOJSONLint. now my data in geojson format seems valid but L.geoJSON in leaflet gives the error that "invalid GeoJSON object".
    – Gary
    Apr 6, 2016 at 6:27
  • So, just to be clear, are you saying that the result of your JSON.stringify(geojson) will parse without errors and display on the map at GeoJSONLint and geojson.io? Could you copy some of that output here? It is probably too long to post the whole thing, but a few features at least might be useful. It is difficult to tell what the problem is just from your code, because for all we know the server could be giving you garbage. Apr 6, 2016 at 6:56
  • Yes and This is the data, i'm getting : [{"type":"Polygon","coordinates":[[[-102.774299999583,27.280200001318],[-88.0033999995794,28.9369000000879],[-87.7235000003129,15.2657999992371],[-95.6770999999717,14.6623000004329],[-103.630699999514,14.0588000016287],[-102.774299999583,27.280200001318]]]}, {"type":"Polygon","coordinates":[[[-102.774299999583,27.280200001318],[-88.0033999995794,28.9369000000879],[-87.7235000003129,15.2657999992371],[-95.6770999999717,14.6623000004329],[-103.630699999514,14.0588000016287],[-102.774299999583,27.280200001318]]]}
    – Gary
    Apr 6, 2016 at 7:22
  • Yes i am getting data between " [ " and " ] " these are first and last char(s). How ever I removed these and getting a arrayList(without '[' & ']' first and last) but now i have to change this ArrayList into GeoJSON format. i have tried JSON.parse() but not getting that data as geoJSON format.
    – Gary
    Apr 6, 2016 at 10:04

1 Answer 1

2

The invalid GeoJSON object error appears to be a product of your edited code, where the temp variable is actually a string, not a GeoJSON object. From your sample output in the comments, it appears that you are getting an array of GeoJson Polygon objects, which L.geoJson can interpret correctly, so your original code (before the edit) should successfully add the data. However, it does not fully initialize the map by giving it a view center and zoom. You can do this when you create the map using map.setView, for example:

var map = L.map('map').setView([20, -95], 5);

or after you create your GeoJSON layer, by fitting the map to the bounds of the layer:

map.fitBounds(jsonLayer.getBounds());

So the following code should both initialize your map and display your data:

var geojson = <%= GJson%>
var map = L.map('map').setView([0, 0], 0);
var jsonLayer = L.geoJson().addTo(map);
jsonLayer.addData(geojson);
map.fitBounds(jsonLayer.getBounds());

Here is an example fiddle using your sample output from the comments:

http://fiddle.jshell.net/nathansnider/sk0ae07u/

(though there are two polygons, they have identical coordinates, so they lie on top of eachother)

1
  • Thank you @nathansnider I add : ` var jsonLayer = L.geoJson().addTo(map); jsonLayer.addData(geojson); map.fitBounds(jsonLayer.getBounds()); ` as you said into the code(before edited code) and i successfully completed with projection on map. once again thank you....
    – Gary
    Apr 6, 2016 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.