eco

package
v0.0.0-...-043deff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2023 License: AGPL-3.0, GPL-3.0-or-later Imports: 13 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CentimetersPerInch = 2.54
View Source
var (
	// List of "factor" files that we will load. Files generally have the form:
	// output__{region}__{factor}.csv
	Factors = []string{"natural_gas", "electricity",
		"hydro_interception", "co2_sequestered",
		"co2_avoided", "co2_storage", "aq_nox_dep", "aq_ozone_dep",
		"aq_nox_avoided", "aq_pm10_dep", "aq_pm10_avoided",
		"aq_sox_dep", "aq_sox_avoided", "aq_voc_avoided", "bvoc"}
)

Functions

func CalcBenefitsWithData

func CalcBenefitsWithData(
	regions []Region,
	rows Fetchable,
	region string,
	speciesdata map[string]map[string]string,
	regiondata map[string][]*Datafile,
	overrides map[string]map[int]string) (map[string]float64, error)

Calculate ecobenefits over an instance in the given backend

Regions are a list of intersecting regions the check. This can be nil or empty only if the "region" parameter is specified

To use a fixed region pass in a valid "region" parameter if this parameter is passed in the regions array will be ignored

Rows is the fetchable set to use

speciesdata is a map to itreecode: region --> otmcode --> itreecode

Static data can be loaded for this via LoadSpeciesMap

regiondata maps regions to factor lists region -> slice of datafiles

Overrides is a map like: region -> species id -> itree code

That allows itree overrides on a per-species/instance level

Note that the ith element of the datafiles slice is the ith factor from eco.Factors

func CalcOneTree

func CalcOneTree(
	factorDataForRegion []*Datafile,
	itreecode string,
	diameter float64,
	factorsum []float64)

Calculate benefits for a single tree

The diameter must be in centimeters

The benefits will be added to the factorsum slice

func DestroyPt

func DestroyPt(p Point)

func FactorArrayToMap

func FactorArrayToMap(factors []float64) map[string]float64

Convert an array of factors into a map by matching up their indicies

func GeosDestroy

func GeosDestroy(geom Geom)

func GetITreeCodesByRegion

func GetITreeCodesByRegion(regionData map[string][]*Datafile) map[string][]string

func GetXYOnSurface

func GetXYOnSurface(g Geom) (float64, float64)

func InitGeos

func InitGeos()

func Intersects

func Intersects(g Geom, p Point) (bool, error)

Determine if p intersects g

func LoadFiles

func LoadFiles(basePath string) map[string][]*Datafile

Load the data files

the relevant data files are stored in the format: output__<regioncode>__<factor_with_csv??>.csv

for example: output__TropicPacXXX__property_value.csv

The returned data stucture has region codes as keys and an array of data files as values. the data file array is indexed by factor id, as determined by the global `Factors`.

For instance, Factors[3] = hydro_interception so the datafile for NoCalXXX region and hydro interception would be:

files = LoadFiles('/path/to/data/folder') hydro_data = files['NoCalXXX'][3]

func LoadSpeciesMap

func LoadSpeciesMap(speciesMasterList string) (map[string]map[string]string, error)

Load the master species map

The output map combines regions with species codes and returns the iTree Code

func OpenDatabaseConnection

func OpenDatabaseConnection(info *DBInfo) (*sql.DB, error)

Types

type DBContext

type DBContext sql.DB

func (*DBContext) ExecSql

func (dbc *DBContext) ExecSql(query string) (Fetchable, error)

func (*DBContext) GetOverrideMap

func (dbc *DBContext) GetOverrideMap() (map[int]map[string]map[int]string, error)

func (*DBContext) GetRegionGeoms

func (dbc *DBContext) GetRegionGeoms() (map[int]Region, error)

func (*DBContext) GetRegionsForInstance

func (dbc *DBContext) GetRegionsForInstance(
	regions map[int]Region, instance int) ([]Region, error)

type DBInfo

type DBInfo struct {
	User     string
	Password string
	Host     string
	Database string
}

type DBRow

type DBRow sql.Rows

func (*DBRow) Close

func (dbr *DBRow) Close() error

func (*DBRow) GetDataWithRegion

func (dbr *DBRow) GetDataWithRegion(
	diameter *float64,
	otmcode *string,
	speciesid *int,
	x *float64,
	y *float64) error

func (*DBRow) GetDataWithoutRegion

func (dbr *DBRow) GetDataWithoutRegion(
	diameter *float64, otmcode *string, speciesid *int) error

func (*DBRow) Next

func (dbr *DBRow) Next() bool

type DataBackend

type DataBackend interface {
	// Given a mapping of region ids to region geoms
	// return the set of regions that intersect the given
	// instance
	GetRegionsForInstance(
		regions map[int]Region, instance int) ([]Region, error)

	// Get a fetchable with the given sql string
	ExecSql(string) (Fetchable, error)

	// Get a map for all of the overrides on the database
	// The map should end up looking something like:
	// instanceid -> region -> species id -> itreecode
	GetOverrideMap() (map[int]map[string]map[int]string, error)

	// Get all itree geometries
	GetRegionGeoms() (map[int]Region, error)
}

Data backends are used to fetch the actual tree data. The main backend right now is a postgres database.

The testing code also provides a mock implementation

type Datafile

type Datafile struct {
	// Breaks are values that form the endpoints of a range
	// of diameter values that all receive the same ecobenefit value.
	Breaks []float64
	Values map[string][]float64
}

A datafile contains a particular set of dbh breaks and data points For example, the datafile: Datafile{[12, 15, 17, 200], [5, 10, 15, 20]} would imply that diameter values of 0-12 have a benefit value of 5, 12-15 have a benefit of 10 value of 10, and so on.

TODO: clarify whether breaks are inclusive/exclusive and at which ends of the range.

func LoadFile

func LoadFile(path string) *Datafile

TODO - Parse these all out to json The data files are in shambles due to the xls exporter we should clean these up once and for all

type Fetchable

type Fetchable interface {
	// This method should only be called on a "region" fetchable
	// object and will get the current record's data
	//
	// The diameter will be in centimeters
	GetDataWithRegion(
		diameter *float64, otmcode *string,
		speciesid *int, x *float64, y *float64) error

	// This method can be called on any fetchable object and
	// will get the current record's data
	//
	// The diameter will be in centimeters
	GetDataWithoutRegion(
		diameter *float64, otmcode *string, speciesid *int) error

	// Closes this fetchable
	Close() error

	// Move to the next item in the internal iterator
	// returns false if there are no more records
	Next() bool
}

Fetchables come out of the database backend and essentially wrap a database rowset

The testing code also provides a mock implementation

type Geom

type Geom struct {
	// contains filtered or unexported fields
}

func MakeGeosGeom

func MakeGeosGeom(wkt string) Geom

Create a new geometry from the given wkt string

The geometry will be 'prepared' to make intersects/contains queries faster

The caller is responsible for destroying the returned geometry with "GeosDestroy"

type Point

type Point struct {
	// contains filtered or unexported fields
}

func CreateGeosPtWithXY

func CreateGeosPtWithXY(x float64, y float64) Point

The caller is responsible for destroying the returned geometry with "DestroyPt"

type Region

type Region struct {
	Code string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL