import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
/home/dhankar/anaconda2/envs/gisenv/bin/python 3.4.5 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:47:47) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] sys.version_info(major=3, minor=4, micro=5, releaselevel='final', serial=0)
import pysal as ps
#import fiona
import numpy as np
import pandas as pd
import geopandas as gpd
!pwd
/home/dhankar/Desktop/Geo_1
ps.examples.available()
['nyc_bikes', '10740', 'south', 'us_income', 'newHaven', 'Polygon', 'arcgis', 'taz', 'networks', 'londonhp', 'columbus', 'snow_maps', 'street_net_pts', 'calemp', 'sacramento2', 'georgia', 'stl', 'burkitt', 'geodanet', 'Point', 'Polygon_Holes', 'wmat', 'juvenile', 'nat', 'sids2', 'chicago', 'Line', 'book', 'baltim', 'mexico', 'clearwater', 'tokyo', 'desmith', 'virginia']
ps.examples.explain('nyc_bikes')
{'description': None, 'explanation': None, 'name': 'nyc_bikes'}
ps.examples.explain('tokyo')
{'description': None, 'explanation': None, 'name': 'tokyo'}
ps.examples.explain('us_income')
{'description': 'Nominal per capita income for the lower 48 US states 1929-2009', 'explanation': ['* states48.gal: queen contiguity weights in GAL format.', '* us48.dbf: attribute data. (k=8)', '* us48.shp: Polygon shapefile. (n=48)', '* us48.shx: spatial index.', '* usjoin.csv: 48 US states nominal per capita income time series 1929-2009.'], 'name': 'us_income'}
print(ps.examples)
#
print(" "*90)
#
print(ps.examples.get_path)
<module 'pysal.examples' from '/home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/__init__.py'> <function get_path at 0x7f5244bd0a60>
!cd /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples
!pwd
/home/dhankar/Desktop/Geo_1
"""
(gisenv) dhankar@dhankar-VPCEB44EN:~/Desktop/Geo_1$
(gisenv) dhankar@dhankar-VPCEB44EN:~/Desktop/Geo_1$ pwd
/home/dhankar/Desktop/Geo_1
(gisenv) dhankar@dhankar-VPCEB44EN:~/Desktop/Geo_1$
(gisenv) dhankar@dhankar-VPCEB44EN:~/Desktop/Geo_1$ cd /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples$ ls
10740 burkitt columbus georgia londonhp newHaven Polygon_Holes sids2 street_net_pts tokyo
arcgis calemp desmith __init__.py mexico nyc_bikes __pycache__ snow_maps taz us_income
baltim chicago examples.txt juvenile nat Point README.txt south test_examples.py virginia
book clearwater geodanet Line networks Polygon sacramento2 stl tmpIvIYa5.gwt wmat
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples$
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples$ cd us_income
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income$ ls
README.md spi_download.csv states48.gal us48.dbf us48.shp us48.shx usjoin.csv
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income$
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income$ pwd
/home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income$
(gisenv) dhankar@dhankar-VPCEB44EN:~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income$
"""
csv_path = ps.examples.get_path('usjoin.csv')
print("CSV Local File Path___ ",csv_path)
CSV Local File Path___ /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income/usjoin.csv
f = ps.open(csv_path)
#
print(" "*10)
#
print(f.header[0]) ## Column 1 of CVS File
#
print(" "*10)
#
print(f.header[5])
#
print(" "*10)
#
print(f.header)
#
print(" "*10)
#print(type(f)) ## <class 'pysal.core.IOHandlers.csvWrapper.csvWrapper'>
Name 1932 ['Name', 'STATE_FIPS', '1929', '1930', '1931', '1932', '1933', '1934', '1935', '1936', '1937', '1938', '1939', '1940', '1941', '1942', '1943', '1944', '1945', '1946', '1947', '1948', '1949', '1950', '1951', '1952', '1953', '1954', '1955', '1956', '1957', '1958', '1959', '1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009']
year_09 = f.by_col('2009')
print(year_09)
[32274, 32077, 31493, 40902, 40093, 52736, 40135, 36565, 33086, 30987, 40933, 33174, 35983, 37036, 31250, 35151, 35268, 47159, 49590, 34280, 40920, 29318, 35106, 32699, 37057, 38009, 41882, 48123, 32197, 46844, 33564, 38672, 35018, 33708, 35210, 38827, 41283, 30835, 36499, 33512, 35674, 30107, 36752, 43211, 40619, 31843, 35676, 42504]
## What are .shp -SHAPE Files ? --- https://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
## PySAL reading -SHAPE Files ---
##
path_shp_file = "/media/dhankar/Dhankar_1/a1_18/a1____GeoSpatialANAL/GIS_DATA/ShapeFiles/tl_2010_02_place10.shp"
print("path_shp_file____",path_shp_file)
#
shp_f = ps.open(path_shp_file)
#
print(type(shp_f))
#
# <class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'>
path_shp_file____ /media/dhankar/Dhankar_1/a1_18/a1____GeoSpatialANAL/GIS_DATA/ShapeFiles/tl_2010_02_place10.shp <class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'>
# Exploring SHAPE File
print(shp_f.header)
#
print(" "*10)
#
print(shp_f.header[0]) # Cant Index --PurePyShpWrapper -- like the - csvWrapper -- File Object above.
#
print(" "*10)
{'Unused4': 0, 'File Code': 9994, 'Unused1': 0, 'BBOX Zmax': 0.0, 'BBOX Mmax': 0.0, 'BBOX Ymin': 51.800809, 'Unused2': 0, 'Unused3': 0, 'BBOX Zmin': 0.0, 'BBOX Xmax': 173.42992099999998, 'File Length': 2069820, 'BBOX Xmin': -176.713098, 'BBOX Mmin': 0.0, 'Version': 1000, 'Shape Type': 5, 'BBOX Ymax': 71.340257, 'Unused0': 0}
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-7-4baa0654dba2> in <module>() 5 print(" "*10) 6 # ----> 7 print(shp_f.header[0]) # Cant Index --PurePyShpWrapper -- like the - csvWrapper -- File Object above. 8 # 9 print(" "*10) KeyError: 0
# The Bounding Boxes
shp_f.bbox
[-176.713098, 51.800809, 173.42992099999998, 71.340257]
# Source -- http://nbviewer.jupyter.org/github/pysal/notebooks/blob/master/notebooks/PySAL_io.ipynb
#
ploygons_list =[]
#
for poly in shp_f:
# print("Iterating over polygon: ", poly.id)
## OK Prints ALL POLYGON ID's
ploygons_list.append(poly)
print(len(ploygons_list))
355
# Polygon - ONE of SHAPE File
poly1 = shp_f[0]
print(type(poly1))
#
print("poly1.area_____",poly1.area)
#
print("poly1.centroid_____",poly1.centroid)
#
print("poly1.holes_____",poly1.holes)
#
print("poly1.perimeter_____",poly1.perimeter)
#
<class 'pysal.cg.shapes.Polygon'> poly1.area_____ 0.01069828264749495 poly1.centroid_____ (-160.50972007241012, 55.316898375212034) poly1.holes_____ [[]] poly1.perimeter_____ 0.49487112845616443
# Writing Own SHAPE Files -- Creating a COPY
#
# Create a NEW DUMMY SHAPE File on local DIR
shp_f1 = ps.open('shp_test.shp', 'w')
#
# Copy the Headers
shp_f1.header = shp_f.header
#
# Write ONE Polygon to New Shape File
#
shp_f1.write(poly1)
shp_f1.close()
#
print(type(shp_f1))
<class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'>
shp_f1 = ps.open('/home/dhankar/Desktop/Geo_1/shp_test.shp', 'w')
print(shp_f1)
## WIP --- Check Below ----
# Polygon - ONE of NEW SHAPE File == shp_f1
poly_1_1 = shp_f1[0]
print(type(poly_1_1))
#
print("poly1.area_____",poly_1_1.area)
#
print("poly1.centroid_____",poly_1_1.centroid)
#
print("poly1.holes_____",poly_1_1.holes)
#
print("poly1.perimeter_____",poly1.perimeter)
#
<pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper object at 0x7ffb8ce817b8>
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-8-dc7b3b81ed54> in <module>() 6 7 # Polygon - ONE of NEW SHAPE File == shp_f1 ----> 8 poly_1_1 = shp_f1[0] 9 print(type(poly_1_1)) 10 # ~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/FileIO.py in __getitem__(self, key) 160 161 def __getitem__(self, key): --> 162 return self.by_row.__getitem__(key) 163 164 @property ~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/FileIO.py in __getitem__(self, key) 143 return self.p.get(self.p.ids[key]) 144 else: --> 145 return self.p.get(key) 146 __call__ = __getitem__ 147 ~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/FileIO.py in get(self, n) 267 prevPos = self.tell() 268 self.seek(n) --> 269 obj = self.__read() 270 self.seek(prevPos) 271 return obj ~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/FileIO.py in __read(self) 310 def __read(self): 311 """ Gets one row from the file handler, and if necessary casts it's objects """ --> 312 row = self._read() 313 if row is None: 314 raise StopIteration ~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/IOHandlers/pyShpIO.py in _read(self) 140 def _read(self): 141 try: --> 142 rec = self.dataObj.get_shape(self.pos) 143 except IndexError: 144 return None AttributeError: 'NoneType' object has no attribute 'get_shape'
dbf_path = ps.examples.get_path('us48.dbf')
print(".dbf Local File Path___ ",dbf_path)
.dbf Local File Path___ /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income/us48.dbf
# explore the .dbf file
dbf_1 = ps.open(dbf_path)
print(type(dbf_1))
#
print(" "*10)
#
print(dbf_1.header[0])
#
print(" "*10)
#
print(dbf_1.header[1])
#
print(" "*10)
#
print(dbf_1.header[2])
#
print(" "*10)
#
print(dbf_1.header[3])
#
print(" "*10)
#
print(dbf_1.header[4:7])
#
print(" "*10)
#
print(dbf_1.field_spec)
#
<class 'pysal.core.IOHandlers.pyDbfIO.DBF'> AREA PERIMETER STATE_ STATE_ID ['STATE_NAME', 'STATE_FIPS', 'SUB_REGION'] [('N', 12, 3), ('N', 12, 3), ('N', 11, 0), ('N', 11, 0), ('C', 25, 0), ('C', 2, 0), ('C', 7, 0), ('C', 2, 0)]
field_spec == A list describing the data types of each field. It is comprised of a list of tuples, each tuple describing a field. The format for the tuples is (“Type”,len,precision). Valid Types are ‘C’ for characters, ‘L’ for bool, ‘D’ for data, ‘N’ or ‘F’ for number.
# Print values from any ONE COLUMN - with COLUMN LABEL
# We get LIST Object
state_id = dbf_1.by_col("STATE_ID")
print(state_id)
#
print(" "*40)
#
print(state_id[0:5])
#
print(" "*40)
#
print(type(state_id))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 51] [1, 2, 3, 4, 5] <class 'list'>
# Get a NUMPY.nd.array from the dbf file columns
ids_np_array = np.array([dbf_1.by_col(ids) for ids in dbf_1.header])
#
t_ids_np_array = np.array([dbf_1.by_col(ids) for ids in dbf_1.header]).T ### TRANSPOSED
#
print(ids_np_array.shape)
#
print("TRANSPOSED Shape ==",t_ids_np_array.shape)
#
print(" "*90)
#
# As seen below - we have "All Columns Values" in a 2D Numpy Array - we dont have COLUMN LABELS
# Not intelligent...
print(ids_np_array)
#
print(" "*90)
#
# We TRANSPOSED the Numpy Array to show it in a DATAFRAME ROWS like format
#
print("TRANSPOSED==",t_ids_np_array)
#
(8, 48) TRANSPOSED Shape == (48, 8) [['20.75' '45.132' '9.571' '21.874' '22.598' '27.966' '16.477' '24.391' '2.794' '25.577' '28.187' '2.677' '15.853' '2.309' '21.606' '13.875' '12.55' '1.392' '0.293' '2.057' '9.932' '29.969' '22.967' '41.533' '11.3' '15.396' '0.553' '6.493' '2.625' '28.041' '10.645' '21.982' '10.512' '18.648' '28.859' '18.031' '12.628' '10.876' '65.06' '30.936' '12.897' '11.871' '14.599' '7.794' '13.517' '11.225' '13.348' '16.928'] ['34.956' '34.527' '18.899' '21.353' '22.746' '21.987' '21.891' '28.529' '8.45' '29.51' '24.787' '8.375' '18.79' '13.105' '23.383' '28.487' '16.841' '5.722' '3.643' '8.419' '16.11' '23.608' '19.992' '42.26' '16.13' '19.999' '4.075' '18.196' '21.881' '22.025' '21.152' '21.105' '31.199' '23.596' '23.257' '25.557' '34.583' '21.46' '64.807' '23.538' '17.238' '22.06' '20.103' '16.127' '20.877' '32.57' '41.085' '40.823'] ['1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '28' '29' '30' '31' '32' '33' '34' '35' '36' '37' '38' '39' '40' '41' '42' '43' '44' '45' '46' '47' '48' '51'] ['1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '28' '29' '30' '31' '32' '33' '34' '35' '36' '37' '38' '39' '40' '41' '42' '43' '44' '45' '46' '47' '48' '51'] ['Washington' 'Montana' 'Maine' 'North Dakota' 'South Dakota' 'Wyoming' 'Wisconsin' 'Idaho' 'Vermont' 'Minnesota' 'Oregon' 'New Hampshire' 'Iowa' 'Massachusetts' 'Nebraska' 'New York' 'Pennsylvania' 'Connecticut' 'Rhode Island' 'New Jersey' 'Indiana' 'Nevada' 'Utah' 'California' 'Ohio' 'Illinois' 'Delaware' 'West Virginia' 'Maryland' 'Colorado' 'Kentucky' 'Kansas' 'Virginia' 'Missouri' 'Arizona' 'Oklahoma' 'North Carolina' 'Tennessee' 'Texas' 'New Mexico' 'Alabama' 'Mississippi' 'Georgia' 'South Carolina' 'Arkansas' 'Louisiana' 'Florida' 'Michigan'] ['53' '30' '23' '38' '46' '56' '55' '16' '50' '27' '41' '33' '19' '25' '31' '36' '42' '09' '44' '34' '18' '32' '49' '06' '39' '17' '10' '54' '24' '08' '21' '20' '51' '29' '04' '40' '37' '47' '48' '35' '01' '28' '13' '45' '05' '22' '12' '26'] ['Pacific' 'Mtn' 'N Eng' 'W N Cen' 'W N Cen' 'Mtn' 'E N Cen' 'Mtn' 'N Eng' 'W N Cen' 'Pacific' 'N Eng' 'W N Cen' 'N Eng' 'W N Cen' 'Mid Atl' 'Mid Atl' 'N Eng' 'N Eng' 'Mid Atl' 'E N Cen' 'Mtn' 'Mtn' 'Pacific' 'E N Cen' 'E N Cen' 'S Atl' 'S Atl' 'S Atl' 'Mtn' 'E S Cen' 'W N Cen' 'S Atl' 'W N Cen' 'Mtn' 'W S Cen' 'S Atl' 'E S Cen' 'W S Cen' 'Mtn' 'E S Cen' 'E S Cen' 'S Atl' 'S Atl' 'W S Cen' 'W S Cen' 'S Atl' 'E N Cen'] ['WA' 'MT' 'ME' 'ND' 'SD' 'WY' 'WI' 'ID' 'VT' 'MN' 'OR' 'NH' 'IA' 'MA' 'NE' 'NY' 'PA' 'CT' 'RI' 'NJ' 'IN' 'NV' 'UT' 'CA' 'OH' 'IL' 'DE' 'WV' 'MD' 'CO' 'KY' 'KS' 'VA' 'MO' 'AZ' 'OK' 'NC' 'TN' 'TX' 'NM' 'AL' 'MS' 'GA' 'SC' 'AR' 'LA' 'FL' 'MI']] TRANSPOSED== [['20.75' '34.956' '1' '1' 'Washington' '53' 'Pacific' 'WA'] ['45.132' '34.527' '2' '2' 'Montana' '30' 'Mtn' 'MT'] ['9.571' '18.899' '3' '3' 'Maine' '23' 'N Eng' 'ME'] ['21.874' '21.353' '4' '4' 'North Dakota' '38' 'W N Cen' 'ND'] ['22.598' '22.746' '5' '5' 'South Dakota' '46' 'W N Cen' 'SD'] ['27.966' '21.987' '6' '6' 'Wyoming' '56' 'Mtn' 'WY'] ['16.477' '21.891' '7' '7' 'Wisconsin' '55' 'E N Cen' 'WI'] ['24.391' '28.529' '8' '8' 'Idaho' '16' 'Mtn' 'ID'] ['2.794' '8.45' '9' '9' 'Vermont' '50' 'N Eng' 'VT'] ['25.577' '29.51' '10' '10' 'Minnesota' '27' 'W N Cen' 'MN'] ['28.187' '24.787' '11' '11' 'Oregon' '41' 'Pacific' 'OR'] ['2.677' '8.375' '12' '12' 'New Hampshire' '33' 'N Eng' 'NH'] ['15.853' '18.79' '13' '13' 'Iowa' '19' 'W N Cen' 'IA'] ['2.309' '13.105' '14' '14' 'Massachusetts' '25' 'N Eng' 'MA'] ['21.606' '23.383' '15' '15' 'Nebraska' '31' 'W N Cen' 'NE'] ['13.875' '28.487' '16' '16' 'New York' '36' 'Mid Atl' 'NY'] ['12.55' '16.841' '17' '17' 'Pennsylvania' '42' 'Mid Atl' 'PA'] ['1.392' '5.722' '18' '18' 'Connecticut' '09' 'N Eng' 'CT'] ['0.293' '3.643' '19' '19' 'Rhode Island' '44' 'N Eng' 'RI'] ['2.057' '8.419' '20' '20' 'New Jersey' '34' 'Mid Atl' 'NJ'] ['9.932' '16.11' '21' '21' 'Indiana' '18' 'E N Cen' 'IN'] ['29.969' '23.608' '22' '22' 'Nevada' '32' 'Mtn' 'NV'] ['22.967' '19.992' '23' '23' 'Utah' '49' 'Mtn' 'UT'] ['41.533' '42.26' '24' '24' 'California' '06' 'Pacific' 'CA'] ['11.3' '16.13' '25' '25' 'Ohio' '39' 'E N Cen' 'OH'] ['15.396' '19.999' '26' '26' 'Illinois' '17' 'E N Cen' 'IL'] ['0.553' '4.075' '28' '28' 'Delaware' '10' 'S Atl' 'DE'] ['6.493' '18.196' '29' '29' 'West Virginia' '54' 'S Atl' 'WV'] ['2.625' '21.881' '30' '30' 'Maryland' '24' 'S Atl' 'MD'] ['28.041' '22.025' '31' '31' 'Colorado' '08' 'Mtn' 'CO'] ['10.645' '21.152' '32' '32' 'Kentucky' '21' 'E S Cen' 'KY'] ['21.982' '21.105' '33' '33' 'Kansas' '20' 'W N Cen' 'KS'] ['10.512' '31.199' '34' '34' 'Virginia' '51' 'S Atl' 'VA'] ['18.648' '23.596' '35' '35' 'Missouri' '29' 'W N Cen' 'MO'] ['28.859' '23.257' '36' '36' 'Arizona' '04' 'Mtn' 'AZ'] ['18.031' '25.557' '37' '37' 'Oklahoma' '40' 'W S Cen' 'OK'] ['12.628' '34.583' '38' '38' 'North Carolina' '37' 'S Atl' 'NC'] ['10.876' '21.46' '39' '39' 'Tennessee' '47' 'E S Cen' 'TN'] ['65.06' '64.807' '40' '40' 'Texas' '48' 'W S Cen' 'TX'] ['30.936' '23.538' '41' '41' 'New Mexico' '35' 'Mtn' 'NM'] ['12.897' '17.238' '42' '42' 'Alabama' '01' 'E S Cen' 'AL'] ['11.871' '22.06' '43' '43' 'Mississippi' '28' 'E S Cen' 'MS'] ['14.599' '20.103' '44' '44' 'Georgia' '13' 'S Atl' 'GA'] ['7.794' '16.127' '45' '45' 'South Carolina' '45' 'S Atl' 'SC'] ['13.517' '20.877' '46' '46' 'Arkansas' '05' 'W S Cen' 'AR'] ['11.225' '32.57' '47' '47' 'Louisiana' '22' 'W S Cen' 'LA'] ['13.348' '41.085' '48' '48' 'Florida' '12' 'S Atl' 'FL'] ['16.928' '40.823' '51' '51' 'Michigan' '26' 'E N Cen' 'MI']]
# Accessing all values within a COLUMN
states = dbf_1.by_col("STATE_NAME")
print(states)
['Washington', 'Montana', 'Maine', 'North Dakota', 'South Dakota', 'Wyoming', 'Wisconsin', 'Idaho', 'Vermont', 'Minnesota', 'Oregon', 'New Hampshire', 'Iowa', 'Massachusetts', 'Nebraska', 'New York', 'Pennsylvania', 'Connecticut', 'Rhode Island', 'New Jersey', 'Indiana', 'Nevada', 'Utah', 'California', 'Ohio', 'Illinois', 'Delaware', 'West Virginia', 'Maryland', 'Colorado', 'Kentucky', 'Kansas', 'Virginia', 'Missouri', 'Arizona', 'Oklahoma', 'North Carolina', 'Tennessee', 'Texas', 'New Mexico', 'Alabama', 'Mississippi', 'Georgia', 'South Carolina', 'Arkansas', 'Louisiana', 'Florida', 'Michigan']
## Extracting Column Data -- with specific Value Types
## Writing a List Comprehension
## NUMERIC Only Columns
num_cols = np.array([dbf_1.by_col(dbf_1.header[i]) for i in range(len(dbf_1.header)) if dbf_1.field_spec[i][0] == 'N']).T
print(type(num_cols))
print(" "*20)
print("SHAPE of the numpy Array ____",num_cols.shape) ## 2 D Array with SHAPE - (48, 4)
print(" "*20)
print(num_cols)
<class 'numpy.ndarray'> SHAPE of the numpy Array ____ (48, 4) [[20.75 34.956 1. 1. ] [45.132 34.527 2. 2. ] [ 9.571 18.899 3. 3. ] [21.874 21.353 4. 4. ] [22.598 22.746 5. 5. ] [27.966 21.987 6. 6. ] [16.477 21.891 7. 7. ] [24.391 28.529 8. 8. ] [ 2.794 8.45 9. 9. ] [25.577 29.51 10. 10. ] [28.187 24.787 11. 11. ] [ 2.677 8.375 12. 12. ] [15.853 18.79 13. 13. ] [ 2.309 13.105 14. 14. ] [21.606 23.383 15. 15. ] [13.875 28.487 16. 16. ] [12.55 16.841 17. 17. ] [ 1.392 5.722 18. 18. ] [ 0.293 3.643 19. 19. ] [ 2.057 8.419 20. 20. ] [ 9.932 16.11 21. 21. ] [29.969 23.608 22. 22. ] [22.967 19.992 23. 23. ] [41.533 42.26 24. 24. ] [11.3 16.13 25. 25. ] [15.396 19.999 26. 26. ] [ 0.553 4.075 28. 28. ] [ 6.493 18.196 29. 29. ] [ 2.625 21.881 30. 30. ] [28.041 22.025 31. 31. ] [10.645 21.152 32. 32. ] [21.982 21.105 33. 33. ] [10.512 31.199 34. 34. ] [18.648 23.596 35. 35. ] [28.859 23.257 36. 36. ] [18.031 25.557 37. 37. ] [12.628 34.583 38. 38. ] [10.876 21.46 39. 39. ] [65.06 64.807 40. 40. ] [30.936 23.538 41. 41. ] [12.897 17.238 42. 42. ] [11.871 22.06 43. 43. ] [14.599 20.103 44. 44. ] [ 7.794 16.127 45. 45. ] [13.517 20.877 46. 46. ] [11.225 32.57 47. 47. ] [13.348 41.085 48. 48. ] [16.928 40.823 51. 51. ]]
str_cols = np.array([dbf_1.by_col(dbf_1.header[i]) for i in range(len(dbf_1.header)) if dbf_1.field_spec[i][0] == 'C']).T
print(type(str_cols))
print(" "*20)
print("SHAPE of the numpy Array ____",str_cols.shape) ## 2 D Array with SHAPE - (48, 4)
print(" "*20)
print(str_cols)
<class 'numpy.ndarray'> SHAPE of the numpy Array ____ (48, 4) [['Washington' '53' 'Pacific' 'WA'] ['Montana' '30' 'Mtn' 'MT'] ['Maine' '23' 'N Eng' 'ME'] ['North Dakota' '38' 'W N Cen' 'ND'] ['South Dakota' '46' 'W N Cen' 'SD'] ['Wyoming' '56' 'Mtn' 'WY'] ['Wisconsin' '55' 'E N Cen' 'WI'] ['Idaho' '16' 'Mtn' 'ID'] ['Vermont' '50' 'N Eng' 'VT'] ['Minnesota' '27' 'W N Cen' 'MN'] ['Oregon' '41' 'Pacific' 'OR'] ['New Hampshire' '33' 'N Eng' 'NH'] ['Iowa' '19' 'W N Cen' 'IA'] ['Massachusetts' '25' 'N Eng' 'MA'] ['Nebraska' '31' 'W N Cen' 'NE'] ['New York' '36' 'Mid Atl' 'NY'] ['Pennsylvania' '42' 'Mid Atl' 'PA'] ['Connecticut' '09' 'N Eng' 'CT'] ['Rhode Island' '44' 'N Eng' 'RI'] ['New Jersey' '34' 'Mid Atl' 'NJ'] ['Indiana' '18' 'E N Cen' 'IN'] ['Nevada' '32' 'Mtn' 'NV'] ['Utah' '49' 'Mtn' 'UT'] ['California' '06' 'Pacific' 'CA'] ['Ohio' '39' 'E N Cen' 'OH'] ['Illinois' '17' 'E N Cen' 'IL'] ['Delaware' '10' 'S Atl' 'DE'] ['West Virginia' '54' 'S Atl' 'WV'] ['Maryland' '24' 'S Atl' 'MD'] ['Colorado' '08' 'Mtn' 'CO'] ['Kentucky' '21' 'E S Cen' 'KY'] ['Kansas' '20' 'W N Cen' 'KS'] ['Virginia' '51' 'S Atl' 'VA'] ['Missouri' '29' 'W N Cen' 'MO'] ['Arizona' '04' 'Mtn' 'AZ'] ['Oklahoma' '40' 'W S Cen' 'OK'] ['North Carolina' '37' 'S Atl' 'NC'] ['Tennessee' '47' 'E S Cen' 'TN'] ['Texas' '48' 'W S Cen' 'TX'] ['New Mexico' '35' 'Mtn' 'NM'] ['Alabama' '01' 'E S Cen' 'AL'] ['Mississippi' '28' 'E S Cen' 'MS'] ['Georgia' '13' 'S Atl' 'GA'] ['South Carolina' '45' 'S Atl' 'SC'] ['Arkansas' '05' 'W S Cen' 'AR'] ['Louisiana' '22' 'W S Cen' 'LA'] ['Florida' '12' 'S Atl' 'FL'] ['Michigan' '26' 'E N Cen' 'MI']]
# Creating a DICTIONARY n then a PANADAS DataFrame from the Numpy Arrays
#
dic_1 = dict([(col, np.array(dbf_1.by_col(col))) for col in dbf_1.header])
df_1 = pd.DataFrame(dic_1)
# Print out Top Five and Bottom Five Observations / Rows
print(df_1.head(5))
print("_ _"*60)
print(" "*60)
print(df_1.tail(5))
AREA PERIMETER STATE_ STATE_ABBR STATE_FIPS STATE_ID STATE_NAME \ 0 20.750 34.956 1 WA 53 1 Washington 1 45.132 34.527 2 MT 30 2 Montana 2 9.571 18.899 3 ME 23 3 Maine 3 21.874 21.353 4 ND 38 4 North Dakota 4 22.598 22.746 5 SD 46 5 South Dakota SUB_REGION 0 Pacific 1 Mtn 2 N Eng 3 W N Cen 4 W N Cen _ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ _ AREA PERIMETER STATE_ STATE_ABBR STATE_FIPS STATE_ID STATE_NAME \ 43 7.794 16.127 45 SC 45 45 South Carolina 44 13.517 20.877 46 AR 05 46 Arkansas 45 11.225 32.570 47 LA 22 47 Louisiana 46 13.348 41.085 48 FL 12 48 Florida 47 16.928 40.823 51 MI 26 51 Michigan SUB_REGION 43 S Atl 44 W S Cen 45 W S Cen 46 S Atl 47 E N Cen
# to get Original Order of Columns - pass the dbf_1.header , LIST , to the PANDAS DF as a LIST index
#
print(type(dbf_1.header))
print(" "*60)
#df_1 = df_1[dbf_1.header]
print(df_1.head(5))
print("_ _"*60)
print(" "*60)
print(df_1.tail(5))
<class 'list'> AREA PERIMETER STATE_ STATE_ABBR STATE_FIPS STATE_ID STATE_NAME \ 0 20.750 34.956 1 WA 53 1 Washington 1 45.132 34.527 2 MT 30 2 Montana 2 9.571 18.899 3 ME 23 3 Maine 3 21.874 21.353 4 ND 38 4 North Dakota 4 22.598 22.746 5 SD 46 5 South Dakota SUB_REGION 0 Pacific 1 Mtn 2 N Eng 3 W N Cen 4 W N Cen _ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ _ AREA PERIMETER STATE_ STATE_ABBR STATE_FIPS STATE_ID STATE_NAME \ 43 7.794 16.127 45 SC 45 45 South Carolina 44 13.517 20.877 46 AR 05 46 Arkansas 45 11.225 32.570 47 LA 22 47 Louisiana 46 13.348 41.085 48 FL 12 48 Florida 47 16.928 40.823 51 MI 26 51 Michigan SUB_REGION 43 S Atl 44 W S Cen 45 W S Cen 46 S Atl 47 E N Cen
print(df_1.describe())
print(" "*60)
print(df_1.describe().T)
AREA PERIMETER STATE_ STATE_ID count 48.000000 48.000000 48.000000 48.000000 mean 17.022792 22.912771 25.000000 25.000000 std 12.475875 10.849594 14.510451 14.510451 min 0.293000 3.643000 1.000000 1.000000 25% 10.367000 17.956500 12.750000 12.750000 50% 14.237000 21.886000 24.500000 24.500000 75% 22.690250 26.289500 37.250000 37.250000 max 65.060000 64.807000 51.000000 51.000000 count mean std min 25% 50% 75% \ AREA 48.0 17.022792 12.475875 0.293 10.3670 14.237 22.69025 PERIMETER 48.0 22.912771 10.849594 3.643 17.9565 21.886 26.28950 STATE_ 48.0 25.000000 14.510451 1.000 12.7500 24.500 37.25000 STATE_ID 48.0 25.000000 14.510451 1.000 12.7500 24.500 37.25000 max AREA 65.060 PERIMETER 64.807 STATE_ 51.000 STATE_ID 51.000
us_income_sh_path = ps.examples.get_path('us48.shp')
print(".dbf Local File Path___ ",us_income_sh_path)
.dbf Local File Path___ /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/us_income/us48.shp
# Visualizing the POLYGONS
#
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
from pysal.contrib.viz import mapping as maps
#
fig = plt.figure()
#
#path_shp_file = "/media/dhankar/Dhankar_1/a1_18/a1____GeoSpatialANAL/GIS_DATA/ShapeFiles/tl_2010_02_place10.shp"
#
#print("path_shp_file____",path_shp_file)
#
shp_f = ps.open(us_income_sh_path)
#
print(type(shp_f))
#
base = maps.map_poly_shp(shp_f)
base.set_facecolor('blue')
base.set_linewidth(0.75)
base.set_edgecolor('0.3') ## 0.8 --- gives Light Greyish edges
ax = maps.setup_ax([base],[shp_f.bbox, shp_f.bbox, shp_f.bbox])
print(type(ax))
fig.add_axes(ax)
plt.show()
#maps.plot_poly_lines(path_shp_file)
<class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'> <class 'matplotlib.axes._subplots.AxesSubplot'>
london_hp = ps.examples.get_path('londonhp.shp')
## This FUNC searches in the "examples" DIR for File Name STRING being passed.
print(".shp Local File Path___ ",london_hp)
.shp Local File Path___ /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/londonhp/londonhp.shp
# Visualizing the POLYGONS
#
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
from pysal.contrib.viz import mapping as maps
#
fig1 = plt.figure()
#
shp_f1 = ps.open(london_hp)
#
print(type(shp_f1))
#
base = maps.map_poly_shp(shp_f1)
base.set_facecolor('blue')
base.set_linewidth(0.75)
base.set_edgecolor('0.3') ## 0.8 --- gives Light Greyish edges
ax1 = maps.setup_ax([base],[shp_f1.bbox, shp_f1.bbox, shp_f1.bbox])
print(type(ax1))
fig1.add_axes(ax1)
plt.show()
#maps.plot_poly_lines(path_shp_file)
<class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'>
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-62-758751c2c697> in <module>() 18 print(type(shp_f1)) 19 # ---> 20 base = maps.map_poly_shp(shp_f1) 21 base.set_facecolor('blue') 22 base.set_linewidth(0.75) ~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/contrib/viz/mapping.py in map_poly_shp(shp, which, bbox) 202 if which == 'all': 203 for shape in shp: --> 204 for ring in shape.parts: 205 xy = np.array(ring) 206 patches.append(xy) AttributeError: 'Point' object has no attribute 'parts'
<matplotlib.figure.Figure at 0x7ffb45156780>
natregimes_shp = ps.examples.get_path('natregimes.shp')
## This FUNC searches in the "examples" DIR for File Name STRING being passed.
print(".shp Local File Path___ ",natregimes_shp)
.shp Local File Path___ /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/nat/natregimes.shp
# Visualizing the POLYGONS
#
%matplotlib inline
import random as rdm
import matplotlib
import matplotlib.pyplot as plt
from pysal.contrib.viz import mapping as maps
#
fig2 = plt.figure(figsize=(9, 9), dpi=100)
#
shp_f2 = ps.open(natregimes_shp)
#
print(type(shp_f2))
#
base = maps.map_poly_shp(shp_f2)
base.set_facecolor('pink')
base.set_linewidth(0.25)
base.set_edgecolor('0.3') ## 0.8 --- gives Light Greyish edges
ax2 = maps.setup_ax([base],[shp_f2.bbox, shp_f2.bbox, shp_f2.bbox])
print(type(ax2))
# examples.get_path('natregimes.shp')
fig2.add_axes(ax2)
plt.show()
<class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'> <class 'matplotlib.axes._subplots.AxesSubplot'>
# Level TWO Visualization with PySAL
# SOURCE --- http://darribas.org/gds_scipy16/ipynb_md/02_geovisualization.html
#
#
net_link = ps.examples.get_path('eberly_net.shp')
net_link_dbf = ps.examples.get_path('eberly_net.dbf')
print(net_link)
print(net_link_dbf)
#
net = ps.open(net_link)
values = np.array(ps.open(net_link.replace('.shp', '.dbf')).by_col('TNODE'))
# Above line of Code == stores VALUES of COLUMN TNODE - from dbase .dbf File
#
pts_link = ps.examples.get_path('eberly_net_pts_onnetwork.shp')
print(pts_link)
pts = ps.open(pts_link)
#
fig_l2 = plt.figure(figsize=(9,9)) ### CHECK --- BOKEH or MATPLOTLIB Figure ?
#
netm = maps.map_line_shp(net)
netc = maps.base_choropleth_unique(netm, values)
#
ptsm = maps.map_point_shp(pts)
ptsm = maps.base_choropleth_classif(ptsm, values)
ptsm.set_alpha(0.5)
ptsm.set_linewidth(0.)
#
ax = maps.setup_ax([netc, ptsm], [net.bbox, net.bbox])
fig.add_axes(ax)
show()
/home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/networks/eberly_net.shp /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/networks/eberly_net.dbf /home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/examples/networks/eberly_net_pts_onnetwork.shp
/home/dhankar/anaconda2/envs/gisenv/lib/python3.4/site-packages/matplotlib/cbook/deprecation.py:106: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. warnings.warn(message, mplDeprecation, stacklevel=1)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-75-37fedf003cd5> in <module>() 27 # 28 ax = maps.setup_ax([netc, ptsm], [net.bbox, net.bbox]) ---> 29 fig.add_axes(ax) 30 show() ~/anaconda2/envs/gisenv/lib/python3.4/site-packages/matplotlib/figure.py in add_axes(self, *args, **kwargs) 951 if a.get_figure() is not self: 952 msg = "The Axes must have been created in the present figure" --> 953 raise ValueError(msg) 954 else: 955 rect = args[0] ValueError: The Axes must have been created in the present figure
# Online TEXAS Shape Files
"""
blockgroup: https://www2.census.gov/geo/tiger/TIGER2010/BG/2010/tl_2010_48_bg10.zip
state: https://www2.census.gov/geo/tiger/TIGER2010/STATE/2010/tl_2010_48_state10.zip
county: https://www2.census.gov/geo/tiger/TIGER2010/COUNTY/2010/tl_2010_48_county10.zip
tract: https://www2.census.gov/geo/tiger/TIGER2010/TRACT/2010/tl_2010_48_tract10.zip
cd: https://www2.census.gov/geo/tiger/TIGER2010/CD/111/tl_2010_48_cd111.zip
block: https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/2010/tl_2010_48_tabblock10.zip
zcta: https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/2010/tl_2010_48_zcta510.zip
"""
tex_shp = "/home/dhankar/Desktop/Geo_1/data/texas.shp"
#
shp_tex = ps.open(tex_shp)
#
print(type(shp_tex))
#
print(" "*10)
#
print(shp_tex.header)
#
print(" "*10)
#
print(shp_tex.header[0]) # Cant Index --PurePyShpWrapper -- like the - csvWrapper -- File Object above.
#
print(" "*10)
<class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'> {'BBOX Mmax': 0.0, 'Unused2': 0, 'File Length': 470462, 'BBOX Ymax': 36.500704, 'BBOX Zmax': 0.0, 'BBOX Zmin': 0.0, 'BBOX Mmin': 0.0, 'Unused3': 0, 'Unused1': 0, 'BBOX Ymin': 25.837163999999998, 'Shape Type': 5, 'BBOX Xmin': -106.645646, 'Unused4': 0, 'File Code': 9994, 'BBOX Xmax': -93.508039, 'Unused0': 0, 'Version': 1000}
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-7-4be29191c85c> in <module>() 24 print(" "*10) 25 # ---> 26 print(shp_tex.header[0]) # Cant Index --PurePyShpWrapper -- like the - csvWrapper -- File Object above. 27 # 28 print(" "*10) KeyError: 0
# The Bounding Boxes
shp_tex.bbox
[-106.645646, 25.837163999999998, -93.508039, 36.500704]
# Source -- http://nbviewer.jupyter.org/github/pysal/notebooks/blob/master/notebooks/PySAL_io.ipynb
# We got only the EXTERNAL STATE Boundary Polygon for TEXAS ?
ploygons_list =[]
#
for poly in shp_tex:
# print("Iterating over polygon: ", poly.id)
## OK Prints ALL POLYGON ID's
ploygons_list.append(poly)
print(len(ploygons_list))
1
# Polygon - ONE of SHAPE File
poly1 = shp_tex[0]
print(type(poly1))
#
print("poly1.area_____",poly1.area)
#
print("poly1.centroid_____",poly1.centroid)
#
print("poly1.holes_____",poly1.holes)
#
print("poly1.perimeter_____",poly1.perimeter)
#
<class 'pysal.cg.shapes.Polygon'> poly1.area_____ 66.05431238211992 poly1.centroid_____ (-99.31713666448327, 31.44721459329732) poly1.holes_____ [[]] poly1.perimeter_____ 56.71194507300427
%matplotlib inline
import numpy as np
import pysal as ps
import random as rdm
from pysal.contrib.viz import mapping as maps
tex_shp_path = "/home/dhankar/Desktop/Geo_1/data/texas.shp"
tex_cnts_shp_path = "/home/dhankar/Desktop/Geo_1/data/texas_counties/tl_2010_48_county10.shp"
tex_dbf_path = '/home/dhankar/Desktop/Geo_1/data/texas.dbf'
tex_cnts_dbf_path = '/home/dhankar/Desktop/Geo_1/data/texas_counties/tl_2010_48_county10.dbf'
tex_dbf = ps.open(tex_dbf_path)
tex_cnts_dbf = ps.open(tex_cnts_dbf_path)
#
# explore the .dbf file
print(type(tex_dbf))
#
print(" "*10)
#
print(tex_dbf.header)
print(tex_cnts_dbf.header)
#
print(" "*60)
#
print(tex_dbf.header[0])
print(tex_cnts_dbf.header[0])
#
print(" "*60)
#
# Get a NUMPY.nd.array from the dbf file columns
#
tex_np_array = np.array([tex_dbf.by_col(ids) for ids in tex_dbf.header]) ## Texas State
t_tex_np_array = np.array([tex_dbf.by_col(ids) for ids in tex_dbf.header]).T ## TRANSPOSED - Texas State
#
#
tex_cnts_np_array = np.array([tex_cnts_dbf.by_col(ids) for ids in tex_cnts_dbf.header]) ## Texas State Counties
t_tex_cnts_np_array = np.array([tex_cnts_dbf.by_col(ids) for ids in tex_cnts_dbf.header]).T ## TRANSPOSED - Texas State Counties
#
#
print(tex_np_array.shape)
print("TRANSPOSED Shape ==",t_tex_np_array.shape)
print(tex_cnts_np_array.shape)
print("TRANSPOSED Shape ==",t_tex_cnts_np_array.shape)
#
#
print(" "*60)
#
# As seen below - we have "All Columns Values" in a 2D Numpy Array - we dont have COLUMN LABELS
# Not intelligent...
print(ids_np_array)
#
print(" "*90)
#
# We TRANSPOSED the Numpy Array to show it in a DATAFRAME ROWS like format
#
print("TRANSPOSED==",t_ids_np_array)
values = np.array(ps.open('/home/dhankar/Desktop/Geo_1/data/texas.dbf').by_col('NAME10'))
types = ['classless', 'unique_values', 'quantiles', 'equal_interval', 'fisher_jenks']
for typ in types:
#
maps.plot_choropleth(tex_shp,values,typ)
#
<class 'pysal.core.IOHandlers.pyDbfIO.DBF'> ['REGION10', 'DIVISION10', 'STATEFP10', 'STATENS10', 'GEOID10', 'STUSPS10', 'NAME10', 'LSAD10', 'MTFCC10', 'FUNCSTAT10', 'ALAND10', 'AWATER10', 'INTPTLAT10', 'INTPTLON10'] ['STATEFP10', 'COUNTYFP10', 'COUNTYNS10', 'GEOID10', 'NAME10', 'NAMELSAD10', 'LSAD10', 'CLASSFP10', 'MTFCC10', 'CSAFP10', 'CBSAFP10', 'METDIVFP10', 'FUNCSTAT10', 'ALAND10', 'AWATER10', 'INTPTLAT10', 'INTPTLON10'] REGION10 STATEFP10 (14, 1) TRANSPOSED Shape == (1, 14) (17, 254) TRANSPOSED Shape == (254, 17)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-21-ca91d3461bb6> in <module>() 52 # As seen below - we have "All Columns Values" in a 2D Numpy Array - we dont have COLUMN LABELS 53 # Not intelligent... ---> 54 print(ids_np_array) 55 # 56 print(" "*90) NameError: name 'ids_np_array' is not defined
# Online TEXAS Shape Files
"""
blockgroup: https://www2.census.gov/geo/tiger/TIGER2010/BG/2010/tl_2010_48_bg10.zip
state: https://www2.census.gov/geo/tiger/TIGER2010/STATE/2010/tl_2010_48_state10.zip
county: https://www2.census.gov/geo/tiger/TIGER2010/COUNTY/2010/tl_2010_48_county10.zip
tract: https://www2.census.gov/geo/tiger/TIGER2010/TRACT/2010/tl_2010_48_tract10.zip
cd: https://www2.census.gov/geo/tiger/TIGER2010/CD/111/tl_2010_48_cd111.zip
block: https://www2.census.gov/geo/tiger/TIGER2010/TABBLOCK/2010/tl_2010_48_tabblock10.zip
zcta: https://www2.census.gov/geo/tiger/TIGER2010/ZCTA5/2010/tl_2010_48_zcta510.zip
"""
tex_shp = "/home/dhankar/Desktop/Geo_1/data/texas.shp"
shp_tex = ps.open(tex_shp)
#
print(type(shp_tex))
"""
# Without Specifically asking for a .shx file --- the package is searching for a .shx file when .shp is passed...
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-3-7233cd10fe38> in <module>()
14 tex_shp = "/home/dhankar/Desktop/Geo_1/data/texas.shp"
15
---> 16 shp_tex = ps.open(tex_shp)
17 #
18 print(type(shp_tex))
~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/IOHandlers/pyShpIO.py in __init__(self, *args, **kwargs)
66 self.dataObj = None
67 if self.mode == 'r' or self.mode == 'rb':
---> 68 self.__open()
69 elif self.mode == 'w' or self.mode == 'wb':
70 self.__create()
~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/IOHandlers/pyShpIO.py in __open(self)
77
78 def __open(self):
---> 79 self.dataObj = shp_file(self.dataPath)
80 self.header = self.dataObj.header
81 self.bbox = self.dataObj.bbox
~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/util/shapefile.py in __init__(self, fileName, mode, shape_type)
219
220 if mode == 'r':
--> 221 self._open_shp_file()
222 elif mode == 'w':
223 if shape_type not in self.SHAPE_TYPES:
~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/util/shapefile.py in _open_shp_file(self)
249 fileName = self.fileName
250 self.fileObj = open(fileName + '.shp', 'rb')
--> 251 self._shx = shx_file(fileName)
252 self.header = _unpackDict(UHEADERSTRUCT, self.fileObj)
253 self.shape = TYPE_DISPATCH[self.header['Shape Type']]
~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/util/shapefile.py in __init__(self, fileName, mode)
440
441 if mode == 'r':
--> 442 self._open_shx_file()
443 elif mode == 'w':
444 self._create_shx_file()
~/anaconda2/envs/gisenv/lib/python3.4/site-packages/pysal/core/util/shapefile.py in _open_shx_file(self)
462
463 self.__isreadable()
--> 464 self.fileObj = open(self.fileName + '.shx', 'rb')
465 self._header = _unpackDict(UHEADERSTRUCT, self.fileObj)
466 self.numRecords = numRecords = (self._header['File Length'] - 50) // 4
FileNotFoundError: [Errno 2] No such file or directory: '/home/dhankar/Desktop/Geo_1/data/texas.shx'
"""
### GET DATA Points out of this FILE only for TEXAS ...
# Visualizing the POLYGONS of NAT.shp
#
%matplotlib inline
import random as rdm
import matplotlib
import matplotlib.pyplot as plt
from pysal.contrib.viz import mapping as maps
#
fig3 = plt.figure(figsize=(9, 9), dpi=100)
#
shp_f3 = ps.open(nat_shp)
#
print(type(shp_f3))
#
base = maps.map_poly_shp(shp_f3)
base.set_facecolor('white')
base.set_linewidth(0.25)
base.set_edgecolor('0.2') ## 0.8 --- gives Light Greyish edges
ax3 = maps.setup_ax([base],[shp_f3.bbox, shp_f3.bbox, shp_f3.bbox])
print(type(ax3))
fig3.add_axes(ax3)
plt.show()
<class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'> <class 'matplotlib.axes._subplots.AxesSubplot'>
#SOURCE -- https://github.com/pysal/notebooks/blob/master/notebooks/PySAL_io.ipynb
# Hold for now
"""
f = figure()
ax = f.add_subplot(111)
poly_collection = viz.map_poly_shp(ps.open(path_shp_file))
poly_collection.set_facecolor('none')
ax.add_collection(poly_collection)
ax.autoscale_view()
ax.set_frame_on(False)
ax.axes.get_yaxis().set_visible(False)
ax.axes.get_xaxis().set_visible(False)
show()
"""
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-43-8c1993580989> in <module>() ----> 1 f = figure() 2 ax = f.add_subplot(111) 3 poly_collection = viz.map_poly_shp(ps.open(path_shp_file)) 4 poly_collection.set_facecolor('none') 5 ax.add_collection(poly_collection) NameError: name 'figure' is not defined
## What are .shp -SHAPE Files ? --- https://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
## PySAL reading -SHAPE Files ---
##
path_shp_file = "/media/dhankar/Dhankar_1/a1_18/a1____GeoSpatialANAL/GIS_DATA/ShapeFiles/tl_2010_02_place10.shp"
print("path_shp_file____",path_shp_file)
#
shp_f = ps.open(path_shp_file)
#
print(type(shp_f))
#
# <class 'pysal.core.IOHandlers.pyShpIO.PurePyShpWrapper'>
## MARKOV based methods - significance in GEOMATICS
# https://brilliant.org/wiki/markov-chains/
# https://en.wikipedia.org/wiki/Markov_chain
# Cross refer the CRAN --
# https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.pdf