#set up shapefile for output w = shapefile.Writer(shapefile.POINT) w.field('DGGSrHEALPix', 'C', '20') w.field('LongiWGS84', 'C', '20') #using 'C' = character = ensures the correct number w.field('LatiWGS84', 'C', '20') w.field('Approx_width', 'C', '20') w.field('DGGS_reso', 'C', '20') w.field('LGAcode', 'C', '20') w.field('Name', 'C', '20') w.field('dggs_cell', 'C', '100') # make an dggs instance rdggs = RHEALPixDGGS() # open a shape file to get your polygons from myFile = r'\\xxxxxxxxxx\LGA_2018_no_multipart.shp' #LGS's can be downloaded from ABS? # read in the file r = shapefile.Reader(myFile) # get the attribute table records (combined with shapes) ie shapeRecords shapeRecs = r.shapeRecords() # function to write a list to a csv file # requires the list and the filename to save it too def write_list_to_file(myList, filename): """Write the list to csv file."""
#import geojson import pygeoj import numpy from auspixdggs.auspixengine.dggs import RHEALPixDGGS rdggs = RHEALPixDGGS() # make an instance def cells_in_poly(bbox, myPoly, resolution, return_cell_obj=False): # returns the cells in the poly and lat long of centroid ''' a function to calculate DGGS cells within a bounding box then check which ones are in the Polygon resolution is the DGGS resolution required - normally 10 myPoly expects a sequence of coordinates ''' # convert the geojson bbox to an AusPIX bounding box nw = (bbox[0], bbox[3]) se = (bbox[2], bbox[1]) #print('nw', nw, 'se', se) # for S region - alternate method work around - needs a list grid of points in the area of interest - then ask for the cell each is in # bbox_myPoints = point_set_from_bounds(resolution, nw, se) # cell_list = [] # for pt in bbox_myPoints: # thiscell = rdggs.cell_from_point(resolution, pt, plane=False) # if thiscell not in cell_list: # cell_list.append(thiscell) # call function to calculate all the cells within the bounding box - this function is not working properly in the S area (southern Tas and Antartica # - use point_set_from_bounds function (above) instead cells = rdggs.cells_from_region(resolution, nw, se,
''' this script uses the DGGS engine to calculate the DGGS cells within a polygon - it reads in a shapefile, but could be adapted to read in other spatial info - eg postGIS - outputs another shapefile for visualisation and integration - could also be adapted to export as csv or triple etc - depends on the 'dggs_in_poly' GA module which does much of the work - see AusPIX enablement folder Git Written by Joseph Bell at Geoscience Australia 2019 ''' import shapefile #to read and write shapefiles import auspixdggs.callablemodules.dggs_in_poly # this is script written by GA - see GitHub AusPix DGGS enablement folder from auspixdggs.auspixengine.dggs import RHEALPixDGGS rdggs = RHEALPixDGGS() # make an instance # identify a shape file to get your polygons from myFile = r'C:\xxxxxxxxxx\AusPIX_DGGS\test_data\testPoly\testPoly3.shp' r = shapefile.Reader(myFile) # read in the file # get the attribute table records (combined with shapes) ie shapeRecords shapeRecs = r.shapeRecords() resolution = 10 # set resolution to be used #set up shapefile for output w = shapefile.Writer(shapefile.POINT) # in this case points == centroids of cells w.field('DGGSrHEALPix', 'C', '20') w.field('LongiWGS84', 'C', '20') #using 'C' = character = ensures the correct number w.field('LatiWGS84', 'C', '20') w.field('DGGS_reso', 'C', '20') w.field('SA1code', 'C', '20') w.field('SA2Name', 'C', '20')
''' this script DGGS enables a csv point file written by Joseph Bell October 2019''' import csv # import csv reader from auspixdggs.auspixengine.dggs import RHEALPixDGGS # import the DGGS rdggs = RHEALPixDGGS() # make an instance of dggs # point to the CSV file myFile = r'C:\Users\u82871\PycharmProjects\doc_scripts\AusPIX_DGGS\test_data\PointFile_example.csv' def write_list_to_file(myList, filename): ''' function to write a CSV from a python list requires the list and the filename to save it to the as inputs''' with open(filename, "w") as outfile: # Write the list to csv file for entries in myList: #cleanup for csv output thisRow = str(entries) #convert to string thisRow = thisRow.replace('[', '') thisRow = thisRow.replace(']', '') thisRow = thisRow.replace("'", "") outfile.write(thisRow) # write outfile.write("\n") # add a new line # variables declarations output = [] # to hold data output to CSV resolution = 10 # # define resolution ’ # open the data file with open(myFile) as csvDataFile: csvReader = csv.reader(csvDataFile) # read the data in
''' f = r'\\xxxxxxxxxxxxxxxxxx\PLACENAMES_2018.csv' output = list() import csv from auspixdggs.auspixengine.dggs import RHEALPixDGGS import math from auspixdggs.auspixengine.utils import my_round # make an instance rdggs = RHEALPixDGGS() # function to write a list to a csv file # requires the list and the filename to save it too def write_list_to_file(myList, filename): """Write the list to csv file.""" with open(filename, "w") as outfile: for entries in myList: outfile.write(entries) # add a return after each line outfile.write("\n") def cleanPosition(pos): pos = pos.replace('(', '')
In practice the DGGS boxes were often too big. Needs adjusting eg a level DGGS 5 box is too big for anything. ''' f = r'\\xxxxxxxx\ACT_grid\ACT_Points.csv' output = list() import csv from auspixdggs.auspixengine.dggs import RHEALPixDGGS import math from auspixdggs.auspixengine.utils import my_round # make an instance rdggs = RHEALPixDGGS() # function to write a list to a csv file # requires the list and the filename to save it too def write_list_to_file(myList, filename): """Write the list to csv file.""" with open(filename, "w") as outfile: for entries in myList: outfile.write(entries) # add a return after each line outfile.write("\n") def cleanPosition(pos):
# -*- coding: utf-8 -*- """ Created on 1st March 2019 @author: Joseph Bell Geoscience Australia """ ''' small module to find the DGGS cell for a point (long, lat) WGS84 The point will be anywhere in the cell returned. ''' from auspixdggs.auspixengine.dggs import RHEALPixDGGS rdggs = RHEALPixDGGS() def latlong_to_DGGS(coords, resolution): # calculate the dggs cell from long and lat thisCell = rdggs.cell_from_point( resolution, coords, plane=False) # false = on the elipsoidal curve # now have a dggs cell for that point print('DGGScell', thisCell) return thisCell if __name__ == '__main__': coordinates = (148.9668333, -35.38275) print('test coords', coordinates) answer = latlong_to_DGGS(coordinates, 10) #10 is the resolution cell area = 2.4408 ha
reads a csv of DGGS corners generated by add_DGGS.py and converts it into a shapefile centroids useing the DGGS cell reference NB: Not sure of the accuracy of shapefile - seems to be working in total confirmity with other datasets - worked into arcGiS Joseph Bell Geoscience Australia ''' import shapefile import csv #import dggs from auspixdggs.auspixengine.dggs import RHEALPixDGGS from auspixdggs.auspixengine.dggs import Cell import auspixdggs.auspixengine.ellipsoids as ellipsoids E = ellipsoids.WGS84_ELLIPSOID_DEG rdggs = RHEALPixDGGS(ellipsoid=E, north_square=1, south_square=2, N_side=3) from geopy import distance # read the file # file with the source data f = r"\\xxxxx\Geonames_withDGGS.csv" #set up shapefile for output w = shapefile.Writer(shapefile.POINT) w.field('ID', 'C', '20') w.field('Name', 'C', '70') w.field('DGGSrHEALPix', 'C', '20') w.field('LongiWGS84', 'C', '20') #using 'C' = character = ensures the correct number
def get_data_list(myFile): #set up shapefile for output w = shapefile.Writer(shapefile.POINT) w.field('DGGSrHEALPix', 'C', '20') w.field('LongiWGS84', 'C', '20') #using 'C' = character = ensures the correct number w.field('LatiWGS84', 'C', '20') w.field('Approx_width', 'C', '20') w.field('DGGS_reso', 'C', '20') w.field('LGAcode', 'C', '20') w.field('Name', 'C', '20') w.field('dggs_cell', 'C', '100') # make an dggs instance rdggs = RHEALPixDGGS() # open a shape file to get your polygons from #myFile = r'\\prod.lan\active\ops\nlib\NLI Reform Project\Place Names Linked Data Project\Meshblocks\1270055001_sa1_2016_aust_shape\SA1_2016_AUST.shp' #myFile = r'\\prod.lan\active\ops\nlib\NLI Reform Project\Place Names Linked Data Project\LGA\SA1_ACT_2016.shp' #myFile = '../test_data/ACT_SA1/SA1_ACT_2016.shp' #SA1's can be downloaded from ABS? # read in the file r = shapefile.Reader(myFile) # get the attribute table records (combined with shapes) ie shapeRecords shapeRecs = r.shapeRecords() # # # function to write a list to a csv file # requires the list and the filename to save it too # # # # set the resolution between about 2 to 12 - bigger numbers mean smaller cells # # resolution = 9 csvOutput = list() # initialise dataList = list() dataList.append(('SA1code', 'SA2name16', 'Areakmsq', 'Num_cells', 'DGGS')) for feature in shapeRecs: # slice of x polygons from the whole shapefile [0:20] newRow = list() # filter out the ACT SA1's if feature.record[8] == 'Australian Capital Territory': # in ACT print(feature.record[0], feature.record[4], feature.record[13]) # thisRecord = feature.record[0:] # the attribute table record # print('len of record', len(thisRecord)) # print(thisRecord) # print('found LGA called ', thisRecord[2]) # polyShape = feature.shape # get the spatial component polyRecord = feature.record # get the attributes record (row) area = float( feature.record[13] ) # needs to point to the area of the polygon in the data sqkm print('Area', area) print(type(area)) # # resolution = 10 # default resolution # vary resolution based on area so bigger areas have bigger cells # if area < 1: # resolution = 12 # elif area > 1 and area < 1000.0: # resolution = 9 # elif area > 1000.0 and area < 30000.0: # resolution = 6 # elif area > 30000: # resolution = 4 print('using resolution ', resolution) # calculate the approx cell width at this resolution thisWidthApprox = rdggs.cell_width( resolution, plane=True ) # note only approx because we are using the planar to estimate elipsoidal print('++++++++++ Cell width approx +++', thisWidthApprox, 'm') thisSA1 = feature.record[0] thisName = feature.record[2] # # call the "call_DGGS" function to return all the DGGS cells (within polygon only) cells_in_poly = call_DGGS.poly_to_DGGS_tool( polyShape, polyRecord, resolution) print('number in polygon = ', len(cells_in_poly)) # # print ('Reducing . . . ') # # reduce to biggest cells # theseReducedCells = call_DGGS.coalesce(cells_in_poly) # #print('reduced cells =', theseReducedCells) # print('1st number reduced cells =', len(theseReducedCells)) # # # reduce again # theseReducedCells = call_DGGS.coalesce(theseReducedCells) # #print('2nd reduced cells =', theseReducedCells) # print('2nd number reduced cells =', len(theseReducedCells)) # # # # reduce again # theseReducedCells = call_DGGS.coalesce(theseReducedCells) # # print('3nd reduced cells =', theseReducedCells) # print('3nd number reduced cells =', len(theseReducedCells)) # # # reduce again # theseReducedCells = call_DGGS.coalesce(theseReducedCells) # # print('3nd reduced cells =', theseReducedCells) # print('4nd number reduced cells =', len(theseReducedCells)) # # # # reduce again # # theseReducedCells = call_DGGS.coalesce(theseReducedCells) # # # print('3nd reduced cells =', theseReducedCells) # # print('5nd number reduced cells =', len(theseReducedCells)) # #newRow.append((feature.record[0], feature.record[4], feature.record[13], len(theseReducedCells), theseReducedCells)) dataList.append( (feature.record[0], feature.record[4], feature.record[13], len(cells_in_poly), cells_in_poly)) #print() #print() #for item in dataList: # print(item) return dataList
''' this script converts a shapefile line feature set into DGGS equivalent - it reads the shapefile then works through line by line figuring out the DGGS cells along that line - the script calls the points that make up the line, then calculates the DGGS cells for those points - if the line has sparse points along it, the user may need to 'densify' first - any duplicated DGGS cells are filtered out - output is as shapefile for visualiation and integration - note python 2 and python 3 versions of shapefile differ - could be adapted to also output csv table ''' import shapefile #to read and write shapefiles from auspixdggs.auspixengine.dggs import RHEALPixDGGS rdggs = RHEALPixDGGS() # make an instance # identify a shape file to get your lines from myFile = r'C:\xxxxxxx\test_data\testPoly\waterways_test.shp' r = shapefile.Reader(myFile) # read in the shapefile file # get the attribute table records (combined with shapes) ie shapeRecords shapeRecs = r.shapeRecords() resolution = 10 # set resolution to be used doneDGGScells = [] #to accumlate a list of completed cells #set up shapefile for output w = shapefile.Writer( shapefile.POLYGON ) # in this case polygons == corners of cells - this is the older verion method w.field('DGGSrHEALPix', 'C', '20') w.field('DGGS_reso', 'C', '20') w.field('thisName', 'C', '20')