##// END OF EJS Templates
Merge pull request #10288 from gnestor/geojson-2...
Matthias Bussonnier -
r23411:c4c40976 merge
parent child Browse files
Show More
@@ -0,0 +1,2 b''
1 * IPython.display has gained a :any:`GeoJSON <IPython.display.GeoJSON>` object.
2 :ghpull:`10288` and :ghpull:`10253`
@@ -680,8 +680,76 b' lib_t1 = """$.getScript("%s", function () {'
680 lib_t2 = """});
680 lib_t2 = """});
681 """
681 """
682
682
683 class GeoJSON(JSON):
683 class GeoJSON(DisplayObject):
684 """GeoJSON expects JSON-able dict
684
685
686 not an already-serialized JSON string.
687
688 Scalar types (None, number, string) are not allowed, only dict containers.
689 """
690 # wrap data in a property, which warns about passing already-serialized JSON
691 _data = None
692
693 def __init__(self, data=None, url_template=None, layer_options=None, url=None, filename=None, metadata=None):
694 """Create a GeoJSON display object given raw data.
695
696 Parameters
697 ----------
698 data : dict or list
699 VegaLite data. Not an already-serialized JSON string.
700 Scalar types (None, number, string) are not allowed, only dict
701 or list containers.
702 url_template : string
703 Leaflet TileLayer URL template: http://leafletjs.com/reference.html#url-template
704 layer_options : dict
705 Leaflet TileLayer options: http://leafletjs.com/reference.html#tilelayer-options
706 url : unicode
707 A URL to download the data from.
708 filename : unicode
709 Path to a local file to load the data from.
710 metadata: dict
711 Specify extra metadata to attach to the json display object.
712
713 Examples
714 --------
715
716 The following will display an interactive map of Mars with a point of
717 interest on frontend that do support GeoJSON display.
718
719 >>> from IPython.display import GeoJSON
720
721 >>> GeoJSON(data={
722 ... "type": "Feature",
723 ... "geometry": {
724 ... "type": "Point",
725 ... "coordinates": [-81.327, 296.038]
726 ... },
727 ... "properties": {
728 ... "name": "Inca City"
729 ... }
730 ... },
731 ... url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
732 ... layer_options={
733 ... "basemap_id": "celestia_mars-shaded-16k_global",
734 ... "attribution" : "Celestia/praesepe",
735 ... "minZoom" : 0,
736 ... "maxZoom" : 18,
737 ... })
738 <IPython.core.display.GeoJSON object>
739
740 In the terminal IPython, you will only see the text representation of
741 the GeoJSON object.
742
743 """
744 self.url_template = url_template
745 self.layer_options = layer_options
746 self.metadata = metadata
747 super(GeoJSON, self).__init__(data=data, url=url, filename=filename)
748
749 def _check_data(self):
750 if self.data is not None and not isinstance(self.data, dict):
751 raise TypeError("%s expects a JSONable dict, not %r" % (self.__class__.__name__, self.data))
752
685 @property
753 @property
686 def data(self):
754 def data(self):
687 return self._data
755 return self._data
@@ -693,11 +761,21 b' class GeoJSON(JSON):'
693 self._data = data
761 self._data = data
694
762
695 def _ipython_display_(self):
763 def _ipython_display_(self):
764 md = {}
765 if self.url_template:
766 md['tileUrlTemplate'] = self.url_template
767 if self.layer_options:
768 md['tileLayerOptions'] = self.layer_options
769 if self.metadata:
770 md.update(self.metadata)
696 bundle = {
771 bundle = {
697 'application/geo+json': self.data,
772 'application/geo+json': self.data,
698 'text/plain': '<jupyterlab_geojson.GeoJSON object>'
773 'text/plain': '<jupyterlab_geojson.GeoJSON object>'
699 }
774 }
700 display(bundle, raw=True)
775 metadata = {
776 'application/geo+json': md
777 }
778 display(bundle, metadata=metadata, raw=True)
701
779
702 class Javascript(TextDisplayObject):
780 class Javascript(TextDisplayObject):
703
781
@@ -28,6 +28,28 b' def test_image_size():'
28 img = display.Image(url=thisurl, unconfined=True)
28 img = display.Image(url=thisurl, unconfined=True)
29 nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_())
29 nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_())
30
30
31
32 def test_geojson():
33
34 gj = display.GeoJSON(data={
35 "type": "Feature",
36 "geometry": {
37 "type": "Point",
38 "coordinates": [-81.327, 296.038]
39 },
40 "properties": {
41 "name": "Inca City"
42 }
43 },
44 url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
45 layer_options={
46 "basemap_id": "celestia_mars-shaded-16k_global",
47 "attribution": "Celestia/praesepe",
48 "minZoom": 0,
49 "maxZoom": 18,
50 })
51 nt.assert_equal(u'<IPython.core.display.GeoJSON object>', str(gj))
52
31 def test_retina_png():
53 def test_retina_png():
32 here = os.path.dirname(__file__)
54 here = os.path.dirname(__file__)
33 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
55 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
General Comments 0
You need to be logged in to leave comments. Login now