Skip to content

Plotting #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
10 changes: 10 additions & 0 deletions geoscript/examples/plot/box_population.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from geoscript.plot.functions import attributesasdict
from geoscript.layer.shapefile import Shapefile
from geoscript.plot.box import box
from geoscript.filter import Filter

if __name__ == '__main__':
layer = Shapefile('D:/gisdata/denver_shapefiles/census_boundaries.shp')
chart = box(attributesasdict(layer, ['BLACK', 'AMERI_ES', 'ASIAN', 'OTHER', 'HISPANIC'], Filter("COUNTYNAME = 'DENVER'")))
chart.show()

8 changes: 8 additions & 0 deletions geoscript/examples/plot/correlation_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from geoscript.processing.sampledata import sampledata
from geoscript.plot import attribute
from geoscript.plot.regression import linearregression

if __name__ == '__main__':
layer = sampledata.get('heights')
chart = linearregression(attribute(layer, 'VALUE'), attribute(layer, 'VALUE'))
chart.show()
11 changes: 11 additions & 0 deletions geoscript/examples/plot/correlation_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from geoscript.processing.sampledata import sampledata
from geoscript.plot import linearregression
from geoscript.plot import attribute
from geoscript.plot import disttopoint

if __name__ == '__main__':
layer = sampledata.get('heights')
pt = layer.features().next().geom.centroid
chart = linearregression(disttopoint(layer, pt.x, pt.y), attribute(layer, 'VALUE'))
chart.savepng("d:\\chart.png")
chart.show()
36 changes: 36 additions & 0 deletions geoscript/examples/plot/distance_distribution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from geoscript.plot import *
from geoscript.layer.shapefile import Shapefile
from geoscript.processing.sampledata import sampledata

if __name__ == '__main__':

#layer = Shapefile('/home/myuser/points.shp')
#===========================================================================
# layer = sampledata.get('heights')
# x = y = ifeat = 0
# for feature in layer.features():
# pt = feature.geom.centroid
# x += pt.x
# y += pt.y
# ifeat += 1
# x /= float(ifeat)
# y /= float(ifeat)
# chart = xybars(frequency(disttopoint(layer, x, y), 10))
# chart.show()
#===========================================================================

#the same thing using the xy function to calculate the mean center
layer = sampledata.get('heights')
x = y = ifeat = 0
xy = xy(layer)
for px,py in xy:
x += px
y += py
ifeat += 1
x /= float(ifeat)
y /= float(ifeat)
chart = xybars(frequency(disttopoint(layer, x, y), 10))
chart.show()



8 changes: 8 additions & 0 deletions geoscript/examples/plot/multiple_bars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from geoscript.plot import *
from geoscript.layer.shapefile import Shapefile

if __name__ == '__main__':
layer = Shapefile('D:/gisdata/denver_shapefiles/census_boundaries.shp')
chart = categorybars(summarize(layer, 'BLKGRP', ['BLACK', 'ASIAN', 'HISPANIC']), stacked=True)
chart.show()

17 changes: 17 additions & 0 deletions geoscript/examples/plot/overlay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from geoscript.plot import *
import random

'''An example that shows how plots can be overlayed'''

if __name__ == '__main__':
xy = [(i,random.random() * 10) for i in range(10)]
chart = curve(xy)
xy2 = [(i,random.random() * 3) for i in range(10)]
chart2 = curve(xy2)
xy3 = [(i,random.random()) for i in range(10)]
chart3 = curve(xy3)
xy4 = [(random.random() * 10,random.random() * 10) for i in range(100)]
chart4 = scatterplot(xy4)
chart.overlay(chart2, chart3, chart4)
chart.show()

9 changes: 9 additions & 0 deletions geoscript/examples/plot/plot_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from geoscript.plot import *
from geoscript.processing.sampledata import sampledata

if __name__ == '__main__':

layer = sampledata.get('heights')
chart = scatterplot(xy(layer))
chart.show()

26 changes: 26 additions & 0 deletions geoscript/examples/plot/semivariogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from geoscript.plot import *
from geoscript.processing.sampledata import sampledata

'''An example that plots a semivariogram cloud'''
import math
import random

if __name__ == '__main__':
layer = sampledata.get('heights')
semivar = []
i= 0
for feature in layer.features():
pt1 = feature.geom.getCoordinates()[0]
value1 = feature.get('VALUE')
i2 = 0
for feature2 in layer.features():
if i2 > i:
pt2 = feature2.geom.getCoordinates()[0]
value2 = feature2.get('VALUE')
semivar.append((pt1.distance(pt2), math.pow(value2-value1,2.)))
i2 += 1
i+=1
# we do not plot all points, as that would mean a large
chart = scatterplot(semivar)
chart.show()

7 changes: 7 additions & 0 deletions geoscript/examples/plot/unique_barchart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from geoscript.processing.sampledata import sampledata
from geoscript.plot import *

if __name__ == '__main__':
layer = sampledata.get('landcover')
chart = categorybars(uniquecounts(layer, 'LANDCOVER'))
chart.show()
7 changes: 7 additions & 0 deletions geoscript/examples/plot/unique_pie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from geoscript.processing.sampledata import sampledata
from geoscript.plot import *

if __name__ == '__main__':
layer = sampledata.get('landcover')
chart = pie(uniquecounts(layer, 'LANDCOVER'))
chart.show()
8 changes: 8 additions & 0 deletions geoscript/plot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from regression import linearregression, powregression
from bars import xybars, categorybars
from pie import pie
from box import box
from curve import curve
from scatterplot import scatterplot
from functions import attribute, attributes, attributesasdict, x, y, disttopoint,\
uniquecounts, frequency, xy, summarize
57 changes: 57 additions & 0 deletions geoscript/plot/bars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from org.jfree.data.xy import XYSeriesCollection, XYSeries
from org.jfree.data.category import DefaultCategoryDataset
from org.jfree.chart import ChartFactory
from org.jfree.chart.plot import PlotOrientation
from geoscript.plot.chart import Chart

def xybars(data, name="xybars"):
'''Creates a xy bars chart.

Input is a list of tuples with (x,y) values'''
series = XYSeries(name);
for x,y in data:
series.add(x,y)
dataset = XYSeriesCollection(series)
chart = ChartFactory.createXYBarChart(None, "X", False,"Y",
dataset, PlotOrientation.VERTICAL, True, True, False)
return Chart(chart)

def intervalbars(data, name="xybars"):
'''Creates a xy bars chart, whit bars defining intervals.
Input is a list of tuples in the form ((lo,hi),y), *lo* and *hi* being
the lower and upper limits of the interval and *y* the count on that interval'''
pass

def categorybars(cat, name="", xlab="", ylab="", stacked=False, tridim=False):
'''Creates a barchart with categorical data.

The input is a dict with category names as keys and numerical values
(the height of the bar) corresponding to that category as values.

Multiple series can be plot, by passing a dict with series names as keys
and dicts as the one described above as values.

By setting *stacked* to true the result will be a stacked bar chart'''
dataset = DefaultCategoryDataset();
for k,v in cat.iteritems():
if isinstance(v, dict):
for k2,v2 in v.iteritems():
dataset.addValue(v2, k2, k)
else:
dataset.addValue(v, "", k)

if tridim:
if stacked:
chart = ChartFactory.createStackedBarChart3D(name, xlab, ylab, dataset,
PlotOrientation.VERTICAL, True, True, True)
else:
chart = ChartFactory.createBarChart3D(name, xlab, ylab, dataset,
PlotOrientation.VERTICAL, True, True, True)
else:
if stacked:
chart = ChartFactory.createStackedBarChart(name, xlab, ylab, dataset,
PlotOrientation.VERTICAL, True, True, True)
else:
chart = ChartFactory.createBarChart(name, xlab, ylab, dataset,
PlotOrientation.VERTICAL, True, True, True)
return Chart(chart)
16 changes: 16 additions & 0 deletions geoscript/plot/box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from org.jfree.data.statistics import DefaultBoxAndWhiskerCategoryDataset
from org.jfree.chart import ChartFactory
from geoscript.plot.chart import Chart

def box(data):
'''creates a box and whiskers plot.
Data is passed as a dict with category names as keys and lists of numbers as values
'''
dataset = DefaultBoxAndWhiskerCategoryDataset()
for name, values in data.iteritems():
dataset.add(values, "", name);
chart = ChartFactory.createBoxAndWhiskerChart("", "", "", dataset, True)
return Chart(chart)



37 changes: 37 additions & 0 deletions geoscript/plot/chart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from javax import swing
from org.jfree.chart import ChartPanel, ChartUtilities
from geoscript import util
from org.jfree.chart.plot import DatasetRenderingOrder
from org.jfree.chart.axis import NumberAxis

class Chart():
'''A class that wraps a JFreeChart Chart object and has method
to easily show or save it'''

def __init__(self,chart):
self.chart = chart
self.datasets = 1

def show(self, size=(500,500)):
panel = ChartPanel(self.chart)
frame = swing.JFrame()
frame.setContentPane(panel)
frame.size = size
frame.visible = True

def savepng(self, filename, size=(500,500)):
ChartUtilities.saveChartAsPNG(util.toFile(filename), self.chart, size[0], size[1])

def overlay(self, *charts):
for chart in charts:
self._overlay(chart)

def _overlay(self, chart):
plot = self.chart.getPlot()
plot.setDataset(self.datasets, chart.chart.getPlot().getDataset())
plot.setRenderer(self.datasets, chart.chart.getPlot().getRenderer())
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD)
yAxis = NumberAxis("")
plot.setRangeAxis(self.datasets, yAxis);
plot.mapDatasetToRangeAxis(self.datasets, self.datasets)
self.datasets += 1
27 changes: 27 additions & 0 deletions geoscript/plot/curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from org.jfree.data.xy import XYSeries, XYSeriesCollection
from org.jfree.chart.plot import PlotOrientation
from org.jfree.chart import ChartFactory
from geoscript.plot.chart import Chart
from org.jfree.chart.renderer.xy import XYSplineRenderer, XYLine3DRenderer

def curve(data, name="", smooth=True, tridim=True):
'''Creates a curve based on a list of (x,y) tuples.

If *smooth* is true, then it uses a spline renderer.

If *tridim* is true, then it creates a 3d-like plot.In this case
smooth value is neglected and no smoothing is added'''
dataset = XYSeriesCollection()
xy = XYSeries(name);
for d in data:
xy.add(d[0], d[1])
dataset.addSeries(xy);
chart = ChartFactory.createXYLineChart(
None, None, None, dataset, PlotOrientation.VERTICAL, True, True, False)
if smooth:
chart.getXYPlot().setRenderer(XYSplineRenderer())
if tridim:
chart.getXYPlot().setRenderer(XYLine3DRenderer())

return Chart(chart)

Loading