mirror of
https://github.com/wassname/geopandas.git
synced 2026-10-05 12:50:08 +08:00
Fix GeoSeries.to_json
This commit is contained in:
1 parent
ff5787c15d
commit
2e3700f77a
2 files changed
+26
-1
No files matched your search
+18
-1
@@ -6,7 +6,7 @@ from pandas import Series, DataFrame
|
||||
from pandas.core.indexing import _NDFrameIndexer
|
||||
from pandas.util.decorators import cache_readonly
|
||||
import pyproj
|
||||
from shapely.geometry import box, shape, Polygon, Point
|
||||
from shapely.geometry import box, mapping, shape, Polygon, Point
|
||||
from shapely.geometry.collection import GeometryCollection
|
||||
from shapely.geometry.base import BaseGeometry
|
||||
from shapely.ops import transform
|
||||
@@ -273,6 +273,23 @@ class GeoSeries(GeoPandasBase, Series):
|
||||
result._invalidate_sindex()
|
||||
return result
|
||||
|
||||
def to_json(self, **kwargs):
|
||||
"""
|
||||
"""
|
||||
def geometry_handler(x):
|
||||
if isinstance(x, BaseGeometry):
|
||||
return {
|
||||
'type': 'Feature',
|
||||
'properties': {},
|
||||
'geometry': mapping(x)
|
||||
}
|
||||
else:
|
||||
return x
|
||||
kwargs.setdefault('default_handler',geometry_handler)
|
||||
kwargs.setdefault('orient','records')
|
||||
features = super(GeoSeries,self).to_json(**kwargs)
|
||||
return '{"type":"FeatureCollection","features":%s}' % features
|
||||
|
||||
#
|
||||
# Implement standard operators for GeoSeries
|
||||
#
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
import json
|
||||
import shutil
|
||||
import tempfile
|
||||
import numpy as np
|
||||
@@ -116,6 +117,13 @@ class TestSeries(unittest.TestCase):
|
||||
self.assertTrue(all(self.g3.geom_equals(s)))
|
||||
# TODO: compare crs
|
||||
|
||||
def test_to_json(self):
|
||||
"""Test whether GeoSeries.to_json works and returns an actual json file."""
|
||||
s = GeoSeries.from_file(tempfilename)
|
||||
json_str = s.to_json()
|
||||
json_dict = json.loads(json_str)
|
||||
# TODO : verify the output is a valid GeoJSON.
|
||||
|
||||
def test_representative_point(self):
|
||||
self.assertTrue(np.alltrue(self.g1.contains(self.g1.representative_point())))
|
||||
self.assertTrue(np.alltrue(self.g2.contains(self.g2.representative_point())))
|
||||
|
||||
Reference in new issue
Block a user