File I/O for spatial data¶
Excel¶
-
BPSpatial.FileIO.Excel.
clear_sheet
(filepath, sheetname)[source]¶ Method for deleting all the values in a excel sheet
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
-
BPSpatial.FileIO.Excel.
create_xlsx
(filepath, sheetname)[source]¶ Method for creating a bland excel file
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
-
BPSpatial.FileIO.Excel.
excel_to_csv
(filepath, sheetname, csvfilepath, encoding='utf-8')[source]¶ Method for converting excel file to csv file
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
- csvfilepath:string
path of csv file to save
encoding: string
-
BPSpatial.FileIO.Excel.
excel_to_list
(excelList, startAt=0)[source]¶ Method for coverting list of xlrd.sheet.Cell to a nested list
- excelList: list of xlrd.sheet.Cell
usually output of read_xlsx function
- startAt: int
start index of rows
if you have a header in the excel file, this would be 1
- resultList: nested list
list of values of .csv file
[[row1val1, row1val2, …],
[row2val1, row2val2, …], …]
-
BPSpatial.FileIO.Excel.
nestedDict_to_xlsx
(nestedDict, filepath, sheetname, colNames=[])[source]¶ Method for converting nested dictionary to excel file
- nestedDict: nested dictionary
dicationry to convert to an excel file
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
- colNames: list of string
a list of column names. The number of column names should be same to that of data variables
-
BPSpatial.FileIO.Excel.
pointGraph_to_xlsx
(pointGraph, filepath, sheetname)[source]¶ Method for converting a point graph to an excel file
- pointGraph: networkx graph
point type networkx graph to convert
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
-
BPSpatial.FileIO.Excel.
read_csv
(filepath)[source]¶ Method for reading a .csv file which separating values with comma(‘ ,’)
- filepath: string
path of the .csv file to handle
- resultList: nested list
list of values of .csv file
[[row1val1, row1val2, …],
[row2val1, row2val2, …], …]
-
BPSpatial.FileIO.Excel.
read_xlsx
(filepath, sheetname)[source]¶ Method for loading an excel file
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
resultXlrd: list of xlrd.sheet.Cell
list of excel values in xlrd.sheet.Cell type
value.ctype
XL_CELL_EMPTY 0 empty string ‘’
XL_CELL_TEXT 1 a Unicode string
XL_CELL_NUMBER 2 float
XL_CELL_DATE 3 float
XL_CELL_BOOLEAN 4 int; 1 means TRUE, 0 means FALSE
XL_CELL_ERROR 5 int representing internal Excel codes; for a text representation, refer to the supplied dictionary error_text_from_code
XL_CELL_BLANK 6 empty string ‘’. Note: this type will appear only when open_workbook(…, formatting_info=True) is used.
value.value
Or output of this function can be an input of ‘excel_to_list’ function
-
BPSpatial.FileIO.Excel.
write_column
(filepath, sheetname, insert, startCol=0, startRow=1)[source]¶ Method for adding a set of columns in an existing excel file
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
- insert: nested list
[ [col1], [col2] , [col3], …]
- startCol: int
index of staring column
if 0, the next colume of the last column of the excel file
- startRow: int
index of staring row
if 0, the next row of the last column of the excel file
1, the first row
-
BPSpatial.FileIO.Excel.
write_row
(filepath, sheetname, insert, startCol=1, startRow=0)[source]¶ Method for adding a set of rows in an existing excel file
- filepath: string
path of the excel file to handle
- sheetname: string
name of sheet to access
- insert: nested list
[[row1], [row2], [row3], …]
- startCol: int
index of staring column
if 0, the next colume of the last column of the excel file
1, A column of excel file
- startRow: int
index of staring row
if 0, the next row of the last column of the excel file
Graph¶
@author: bspark
-
class
BPSpatial.FileIO.Graph.
Point
[source]¶ class to handle point-type graph that only has nodes without edges
-
static
fromCsv
(csvPath, geometryKey, header=0, columns=[], decimals=6)[source]¶ Method for creating a networkx graph from excel file
- csvPath: string
path of csv file to access
- geometryKey: tuple
key of geometry(x,y) or (lng, lat) in columns or header list
- header: 0 or None
0, if data contains column names, None, otherwise
- columns: list of tuple
column name and type [(col1,dtype), (col2, dtype), …]
- graph: networkx graph
a point-type graph, only having nodes
-
static
fromExcel
(excelPath, sheetName, geometryKey, header, columns, decimals=6)[source]¶ Method for creating a networkx graph from excel file
- excelPath: string
path of excel file to access
- sheetName: string
name of sheet name to access
- geometryKey: tuple
key of geometry(x,y) or (lng, lat) in columns or header list
- header: 0 or None
0, if data contains column names, None, otherwise
- columns: list of tuple
column name and type [(col1,dtype), (col2, dtype), …]
- graph: networkx graph
a point-type graph, only having nodes
-
static
fromShapefile
(shpPath, attrs=[], decimals=-1)[source]¶ Method for importing a shapefile and converting it to a newtorkx graph using geopandas to avoid of removal of points with the same coordinates
- shpPath: string
path of shapefile to handle
- attrs: list of string
attributes to add to a graph from the input shp file
- graph: networkx Graph
a graph only with nodes, not edges graph.nodes - key:(x, y, ind), value:{column:value}
-
static
fromShapefile_nx
(shpPath, attrs=[], decimals=-1)[source]¶ Method for importing a shapefile and converting it to a newtorkx graph using networkx. This may have a problem when the data has duplicate points
- shpPath: string
path of shapefile to handle
- attrs: list of string
attributes to add to a graph from the input shp file
- graph: networkx Graph
a graph only with nodes, not edges graph.nodes - key:(x, y, ind), value:{column:value}
-
static
-
class
BPSpatial.FileIO.Graph.
Polyline
[source]¶ Class to handle polyline-type graph that has both nodes and edges. But nodes has no attributes.
-
static
fromShapefile
(shpPath, attrs=[], decimals=6)[source]¶ Method for importing a shapefile and converting it to a newtorkx graph using networkx. This may have a problem when the data has duplicate points
- shpPath: string
path of shapefile to handle
- attrs: list of string
attributes to add to a graph from the input shp file
- decimals: int
number of decimals to round
- graph: networkx Graph
a graph only with nodes, not edges graph.nodes - key:((x1, y2), (x2,y2)) value:{column:value}
-
static
RTree¶
@author: bspark
-
BPSpatial.FileIO.RTree.
lineRect
(lineGraph, node1, node2)[source]¶ Method for generating rectangle for a edge using coordinates of vertises
- lineGraph: networkx graph
graph containing lines
- node1: tuple(x, y)
starting point of an edge
- node2: tuple(x, y)
ending point of an edge
tuple(float, float, float, float) bounding box of an edge
-
BPSpatial.FileIO.RTree.
pointRect
(coord)[source]¶ Method for generating rectangle for a point
- coord: tuple or list of two values
x and y coordinates of a point
boundary box of a point
-
BPSpatial.FileIO.RTree.
rtree_point
(graph, indField='Ind')[source]¶ Method for creating a rtree index and inserting records using networkx graph
- graph: networkx graph
graph containing points
- indField: string
graph attribute for ind of the index
- idxPoint rtree.Index
Index of points
- pointIdCoordDict dictionary
Index values (key:index, value:tuple(x, y))
-
BPSpatial.FileIO.RTree.
rtree_polyline
(polylineGraph, indField='Ind')[source]¶ Method for creating a rtree Index object and inserting records into it
- polylinGraph: networkx graph
graph containing edges
- indField: string
graph attribute for ind of the index
- idxLine rtree.Index
Index of lines
- edgeIdCoordDict dictionary
Index values (key:index, value:tuple(tuple(x, y), tuple(x,y)))
Shapefile¶
-
class
BPSpatial.FileIO.SHP.
Point
[source]¶ Class for creating a shapefile of point data
-
static
fromGraph
(pntGraph, shpPath)[source]¶ Method that creates a point shapefile from a networkx graph
type of shapefile - https://en.wikipedia.org/wiki/Shapefile
Example - https://glenbambrick.com/2016/01/09/csv-to-shapefile-with-pyshp/
- pntGraph: neworkx.Graph
a graph only has nodes object, not edges
- shpPath: string
path of shapefile to handle
-
static
new
(shpPath, fields, records, geometryKey)[source]¶ Method that creates a point shapefile
type of shapefile - https://en.wikipedia.org/wiki/Shapefile
Example - https://glenbambrick.com/2016/01/09/csv-to-shapefile-with-pyshp/
- shpPath: string
path of shapefile to handle
- fields: list of tuple
list of data fields ex) [tuple(field name, type)]
- records: list of tuple
dataset ex) [(val11, val12, …),(val21, val22, …)]
- geometryKey:tuple
geometry keys, location of latitude and longitude in the records ex) tuple(longitude, latitude) or tuple(x, y)
-
static
-
class
BPSpatial.FileIO.SHP.
Polyline
[source]¶ Class for creating a shapefile of polyline data
-
static
fromGraph
(polylineGraph, shpPath)[source]¶ Method that creates a polyline shapefile
type of shapefile - https://en.wikipedia.org/wiki/Shapefile
- polylineGraph: neworkx.Graph
polyline type graph that includes edges information
- shpPath: string
path of shapefile to handle
-
static
new
(shpPath, polylineGraph, fields, records, geometryKey)[source]¶ Method that creates a polyline shapefile
type of shapefile - https://en.wikipedia.org/wiki/Shapefile
Example - https://glenbambrick.com/2016/01/09/csv-to-shapefile-with-pyshp/
- shpPath: string
path of shapefile to handle
- polylineGraph: networkx graph
polyline type graph which including coordinates of vertices
- fields: list of tuple
list of data fields ex) [tuple(field name, type)]
- records: list of tuple
dataset ex) [(val11, val12, …),(val21, val22, …)]
- geometryKey: tuple
geometry keys, location of two end nodes in the records ex) tuple(tuple(longitude, latitude), tuple(longitude, latitude))
-
static
-
class
BPSpatial.FileIO.SHP.
Projection
[source]¶ Handles with projection related functions
-
copyCRS
(shpPathTo)[source]¶ Method for copying the crs of existing .prj file to another shp file
- shpPathFrom: string
shp file path that you copies the crs from
- shpPathTo: string
shp file path to add the copied crs
-
defineProjection
(crsCode, crsType)[source]¶ Method that creates a .proj file for shapefile
- shpPath: string
path of shapefile to handle
- crsCode: int
coordinate reference system code
- crsType: string
‘epsg’ or ‘sr-org’. The others can raise an error.
-
getPROJ4
(crsType)[source]¶ Method to get ‘Proj4’ type projection information from spatialreference.org
This is utilized when implementing project()
The output is a dictionray which is standard crs form
- crsCode: int
coordinate reference system code
- crsType: string
‘epsg’, ‘esri’, or ‘sr-org’. The others can raise an error.
- crsDict: dictionary
coordinate reference system in dictionary type
-
getWKT_PRJ
(crsType)[source]¶ Method to get ‘Human-Readable OGC WKT’ type projection information from spatialreference.org
This is utilized when creating .prj file
Ex) https://glenbambrick.com/2015/08/09/prj/
- crsCode: int
coordinate reference system code
- crsType: string
‘epsg’, ‘esri’, or ‘sr-org’. The others can raise an error.
- crsStr: string
coordinate reference system in string
-