I did not know geojson. But from the perspective of the coordinate systems this does not fit together:
1.
longs = range(-180, 181, ...)
lats = range(84, -81, ...)
2.
Proj(proj='utm', zone='56H', ellps='WGS84')
1: you use a geographic coordinat system. Valid values are
x -180 to + 180
y -90 to +90
You use WGS. WGS is (the most important part of) a geographic coordinate system (GCS). A geographic coordinate system uses a three-dimensional spherical surface to define locations on the earth. A point is referenced by its longitude and latitude values.
2: you use a projected coordinate system. Valid values are e.g.:
x 123456 (or 56123456 with zone identifyer) y 55123456
A projected coordinate system (PCS) is defined on a flat, two-dimensional surface. A map projection converts features between a spherical surface and a flat surface. The UTM coordinate sytem use WGS as its spherical surface. But the coordinates on the spherical surface are projected to the flat surface. If you work with UTM you work with coordinates on the flat surface.
You can use Turf.js to calculate the centroids of the polygons.
Just include it in your HTML head. In my case, I am using the CDN:
<script src="https://unpkg.com/@turf/turf@3.5.2/turf.min.js"></script>
In the following code I am using onEachFeature to check whether the current feature is a polygon, since it would not make sense to convert points. If it is a polygon I am using Turf to calculate its centroid, which gives me a lat and lon, which can be used to add a new marker.
var geojson = L.geoJson(MyLayer,{
onEachFeature: function(feature,layer){
if (feature.geometry.type === 'Polygon') {
console.log('Polygon detected');
var centroid = turf.centroid(feature);
var lon = centroid.geometry.coordinates[0];
var lat = centroid.geometry.coordinates[1];
L.marker([lat,lon]).addTo(map);
}
}
});
geojson.addTo(map);
I have tested this with a GeoJSON including points and polyons.
The original dataset looks like this:
When the above code is executed then you see that the points remain points and the polygons are transformed to centroids, without ever changing the original dataset.
Of course you would not show both, I just did it to show that the example works! You would only show the points.
You can use this with L.CircleMarker as well, I just used L.marker, because it was a quicker way to demonstrate how this works. I also omitted your popup to shorten the code, but it looks like you will not even have to change it.
Best Answer
What you need is to convert your data to a GeoJSON
Feature
orFeatureCollection
. The GeoJSON geometries,Point
,Polygon
,MultiPolygon
, et cetera, don't support attributes likeipAddress
andscore
, in your case. You need to define aFeature
which has both ageometry
andattributes
.To modify the example from Wikipedia, this is what a GeoJSON
Feature
would look like in your case:And this would be an example
FeatureCollection
, in which you have multiple geometries with the attributes you specified.You may have noticed that JSON/GeoJSON syntax is similar to Python dictionaries and lists. In Python, you can directly dump a Python dictionary, with or without nested lists and dictionaries, into a JSON/GeoJSON file using the
json
module. Example:Here is the documentation on the
json
module for Python 2.