pharo-GIS / Shapes

Smalltalk package to read and view GIS data in ESRI shapefile format
MIT License
7 stars 1 forks source link
esri gis natural-earth-vector pharo pharo-smalltalk shapefile smalltalk

Description

Smalltalk package to read and view GIS data in ESRI Shapefile format, originally written by Hans Baveco and forked to work with Pharo.

Installation

EpMonitor disableDuring: [ 
    Metacello new
        onWarningLog;
        repository: 'github://pharo-GIS/Shapes/repository';
        baseline: 'Shapes';
        load ].

Usage

You can try it with your own .shp files or download a sample data set from Natural Earth project using the following example:

| shpE legend urlRoot urlESRIFilePrefix urlESRIFileShp urlPath response fileRef |

" Download Shapefile resources "
urlRoot := 'https://github.com/nvkelso/natural-earth-vector/tree/v5.1.1/110m_cultural/'.
urlESRIFilePrefix := 'ne_110m_populated_places'.
urlESRIFileShp := urlESRIFilePrefix , '.shp'.
urlPath := urlRoot , urlESRIFilePrefix.

#('.shx' '.dbf' '.shp' '.cpg' '.prj') do: [ : ext |
  ZnClient new
   url: (urlPath , ext) asZnUrl;
   queryAt: 'raw' put: 'true';
   numberOfRetries: 2;
   enforceHttpSuccess: true;
   downloadTo: urlESRIFilePrefix , ext;
   get ].

" Load and display it in Morphic "
shpE := ShapeEnsemble fromFile: urlESRIFileShp.
" List data fields "
shpE dataFields inspect.
" List all shape records "
shpE shapeRecords inspect.
" Set the current attribute "
shpE attribute: 'NAME'.

legend := ColorLegend mapValuesToRandom: shpE valuesOfCurrentAttribute.
shpE legend: legend.

shpE displayMorphic.

If you already downloaded the files in your Pharo working directory:

| urlESRIFilePrefix urlESRIFileShp shpE legend |
urlESRIFileShp := FileSystem workingDirectory / 'natural-earth-vector/10m_cultural/ne_10m_admin_0_countries.shp'.
shpE := ShapeEnsemble fromFile: urlESRIFileShp.
shpE attribute: 'CONTINENT'.
legend := ColorLegend mapValuesToRandom: shpE valuesOfCurrentAttribute.
shpE legend: legend.
shpE displayMorphic

Sample outputs

Follows some example outputs using different attributes for the NaturalEarth data set:

CONTINENT

an ImageMorph(55801344)

SUBREGION

an ImageMorph(705029120)

REGION_UN

an ImageMorph(911508224)

NAME_LONG

an ImageMorph(1002761984)