55import datetime
66import logging
77import os
8+ import shapely .geometry
89
910# External Imports
1011import pandas as pd
@@ -146,6 +147,25 @@ def to_csv(data, product, product_subtype, location_type=None, output_dir=None):
146147 logging .info ("CSV generated to '{}'." .format (output_dir + '/' + file_name ))
147148
148149
150+ def get_geom_center (geom ):
151+
152+ if hasattr (geom , "center" ):
153+ if isinstance (geom .center , shapely .geometry .MultiPolygon ):
154+ return {"latitude" : geom .center .centroid .y , "longitude" : geom .center .centroid .x }
155+
156+ elif isinstance (geom .center , shapely .geometry .Point ):
157+ return {"latitude" : geom .center .y , "longitude" : geom .center .x }
158+
159+ else :
160+ raise NotImplementedError
161+
162+ elif hasattr (geom , "y" ) and hasattr (geom , "x" ):
163+ return {"latitude" : geom .y , "longitude" : geom .x }
164+
165+ return {"latitude" : None , "longitude" : None }
166+
167+
168+
149169def format_adaptation_detail (data ):
150170 """Reformat the list of data to Adaptation Detail format
151171
@@ -155,10 +175,12 @@ def format_adaptation_detail(data):
155175 A pandas formatted DataFrame
156176 """
157177 df = pd .DataFrame ([vars (o ) for o in data ]).explode ('type' ).explode ('scenario' ).reset_index (drop = True )
158- df .drop (["serving" , "geometry" ], axis = 1 , inplace = True )
159178 df ['adaptationId' ] = df ['adaptationId' ].astype ('Int64' ).apply (str )
160179 df ['returnPeriod' ] = df ['returnPeriod' ].astype ('Int64' ).apply (str )
161- return df [['adaptationId' , 'name' , 'type' , 'scenario' , 'conveyance' , 'returnPeriod' ]]
180+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
181+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
182+
183+ return df [['adaptationId' , 'name' , 'type' , 'scenario' , 'conveyance' , 'returnPeriod' , 'latitude' , 'longitude' ]]
162184
163185
164186def format_adaptation_summary (data ):
@@ -410,7 +432,6 @@ def format_historic_event(data):
410432 A pandas formatted DataFrame
411433 """
412434 df = pd .DataFrame ([vars (o ) for o in data ])
413- df .drop (["geometry" ], axis = 1 , inplace = True )
414435 if not df ['properties' ].isna ().values .all ():
415436 df = pd .concat ([df .drop (['properties' ], axis = 1 ), df ['properties' ].apply (pd .Series )], axis = 1 )
416437 df ['eventId' ] = df ['eventId' ].astype ('Int64' ).apply (str )
@@ -425,8 +446,11 @@ def format_historic_event(data):
425446 df .drop (['properties' ], axis = 1 , inplace = True )
426447 df ['propertiesTotal' ] = pd .NA
427448 df ['propertiesAffected' ] = pd .NA
449+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
450+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
428451
429- return df [['eventId' , 'name' , 'month' , 'year' , 'returnPeriod' , 'type' , 'propertiesTotal' , 'propertiesAffected' ]]
452+ return df [['eventId' , 'name' , 'month' , 'year' , 'returnPeriod' , 'type' , 'propertiesTotal' ,
453+ 'propertiesAffected' , 'latitude' , 'longitude' ]]
430454
431455
432456def format_historic_summary_property (data ):
@@ -567,9 +591,11 @@ def format_location_detail_property(data):
567591 df ['state_fips' ] = df ['state_fips' ].astype ('Int64' ).apply (str ).apply (lambda x : x .zfill (2 ))
568592 df ['footprintId' ] = df ['footprintId' ].astype ('Int64' ).apply (str )
569593 df ['elevation' ] = df ['city_fips' ].apply (str )
594+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
595+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
570596 return df [['fsid' , 'streetNumber' , 'route' , 'city_fips' , 'city_name' , 'zipCode' , 'neighborhood_fips' ,
571597 'neighborhood_name' , 'tract_fips' , 'county_fips' , 'county_name' , 'cd_fips' ,
572- 'cd_name' , 'state_fips' , 'state_name' , 'footprintId' , 'elevation' , 'fema' ]]
598+ 'cd_name' , 'state_fips' , 'state_name' , 'footprintId' , 'elevation' , 'fema' , 'latitude' , 'longitude' ]]
573599
574600
575601def format_location_detail_neighborhood (data ):
@@ -612,8 +638,10 @@ def format_location_detail_neighborhood(data):
612638 df ['city_fips' ] = df ['city_fips' ].astype ('Int64' ).apply (str )
613639 df ['county_fips' ] = df ['county_fips' ].astype ('Int64' ).apply (str )
614640 df ['state_fips' ] = df ['state_fips' ].astype ('Int64' ).apply (str ).apply (lambda x : x .zfill (2 ))
641+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
642+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
615643 return df [['fsid' , 'name' , 'city_fips' , 'city_name' , 'county_fips' , 'county_name' , 'subtype' ,
616- 'state_fips' , 'state_name' ]]
644+ 'state_fips' , 'state_name' , 'latitude' , 'longitude' ]]
617645
618646
619647def format_location_detail_city (data ):
@@ -666,8 +694,10 @@ def format_location_detail_city(data):
666694 df ['county_fips' ] = df ['county_fips' ].astype ('Int64' ).apply (str )
667695 df ['neighborhood_fips' ] = df ['neighborhood_fips' ].astype ('Int64' ).apply (str )
668696 df ['state_fips' ] = df ['state_fips' ].astype ('Int64' ).apply (str ).apply (lambda x : x .zfill (2 ))
697+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
698+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
669699 return df [['fsid' , 'name' , 'lsad' , 'zipCode' , 'neighborhood_fips' , 'neighborhood_name' ,
670- 'county_fips' , 'county_name' , 'state_fips' , 'state_name' ]]
700+ 'county_fips' , 'county_name' , 'state_fips' , 'state_name' , 'latitude' , 'longitude' ]]
671701
672702
673703def format_location_detail_zcta (data ):
@@ -710,7 +740,10 @@ def format_location_detail_zcta(data):
710740 df ['city_fips' ] = df ['city_fips' ].astype ('Int64' ).apply (str )
711741 df ['county_fips' ] = df ['county_fips' ].astype ('Int64' ).apply (str )
712742 df ['state_fips' ] = df ['state_fips' ].astype ('Int64' ).apply (str ).apply (lambda x : x .zfill (2 ))
713- return df [['fsid' , 'name' , 'city_fips' , 'city_name' , 'county_fips' , 'county_name' , 'state_fips' , 'state_name' ]]
743+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
744+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
745+ return df [['fsid' , 'name' , 'city_fips' , 'city_name' , 'county_fips' , 'county_name' , 'state_fips' ,
746+ 'state_name' , 'latitude' , 'longitude' ]]
714747
715748
716749def format_location_detail_tract (data ):
@@ -744,7 +777,9 @@ def format_location_detail_tract(data):
744777 df ['fsid' ] = df ['fsid' ].apply (str )
745778 df ['county_fips' ] = df ['county_fips' ].astype ('Int64' ).apply (str )
746779 df ['state_fips' ] = df ['state_fips' ].astype ('Int64' ).apply (str ).apply (lambda x : x .zfill (2 ))
747- return df [['fsid' , 'fips' , 'county_fips' , 'county_name' , 'state_fips' , 'state_name' ]]
780+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
781+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
782+ return df [['fsid' , 'fips' , 'county_fips' , 'county_name' , 'state_fips' , 'state_name' , 'latitude' , 'longitude' ]]
748783
749784
750785def format_location_detail_county (data ):
@@ -797,8 +832,10 @@ def format_location_detail_county(data):
797832 df ['zipCode' ] = df ['zipCode' ].astype ('Int64' ).apply (str )
798833 df ['cd_fips' ] = df ['cd_fips' ].astype ('Int64' ).apply (str )
799834 df ['state_fips' ] = df ['state_fips' ].astype ('Int64' ).apply (str ).apply (lambda x : x .zfill (2 ))
835+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
836+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
800837 return df [['fsid' , 'city_fips' , 'city_name' , 'zipCode' , 'fips' , 'isCoastal' , 'cd_fips' ,
801- 'cd_name' , 'state_fips' , 'state_name' ]]
838+ 'cd_name' , 'state_fips' , 'state_name' , 'latitude' , 'longitude' ]]
802839
803840
804841def format_location_detail_cd (data ):
@@ -832,7 +869,9 @@ def format_location_detail_cd(data):
832869 df ['fsid' ] = df ['fsid' ].apply (str )
833870 df ['county_fips' ] = df ['county_fips' ].astype ('Int64' ).apply (str )
834871 df ['state_fips' ] = df ['state_fips' ].astype ('Int64' ).apply (str ).apply (lambda x : x .zfill (2 ))
835- return df [['fsid' , 'district' , 'county_fips' , 'county_name' , 'state_fips' , 'state_name' ]]
872+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
873+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
874+ return df [['fsid' , 'district' , 'county_fips' , 'county_name' , 'state_fips' , 'state_name' , 'latitude' , 'longitude' ]]
836875
837876
838877def format_location_detail_state (data ):
@@ -845,7 +884,9 @@ def format_location_detail_state(data):
845884 """
846885 df = pd .DataFrame ([vars (o ) for o in data ])
847886 df ['fsid' ] = df ['fsid' ].apply (str )
848- return df [['fsid' , 'name' , 'fips' ]]
887+ df ['geometry' ] = df ['geometry' ].apply (get_geom_center )
888+ df = pd .concat ([df .drop (['geometry' ], axis = 1 ), df ['geometry' ].apply (pd .Series )], axis = 1 )
889+ return df [['fsid' , 'name' , 'fips' , 'latitude' , 'longitude' ]]
849890
850891
851892def format_location_summary_property (data ):
0 commit comments