Stewart’s

Show Only ...
Maps - Photos - Videos

Fast Reverse Geocoding Using R and a Local PostGIS Database

Most states like New York have Street Address Management system where you can download every address in the state as a Shapefile then upload it to a locally running PostGIS database.

library(tidyverse)
library(RPostgreSQL)
library(sf)

con <- DBI::dbConnect(RPostgres::Postgres(), dbname='gis', host='localhost', 
                      port=5432, user='postgres', password='xxxxx')

# function that uses PostGIS query to return the closest
# point in the SAM database
reverseGeocode <- function(geom) {
  # convert sf geom object to text
  geom <- st_as_text(geom)
  
  # query
  sql <- str_c("
SELECT 
  *,
  \"Shape\" <-> 'SRID=26918;", geom, "'::geometry AS dist
FROM
  nys_sam_points
ORDER BY
  dist
LIMIT 1;")
  
  query <- dbGetQuery(con, sql)
  
  str_c(query$addresslabel, 
        ', ',
        query$citytownname,
        ', NY ',
        query$zipcode
        ) %>% return
}

# obtain centroids for each county
ny.county.centroids <- tigris::county_subdivisions('ny') %>% 
  st_centroid()

# get addresses of properties by reverse gecoding
ny.county.centroids %>%
  st_transform(26918) %>%   # use same projection 
                            # as your postgis database
  mutate(
    # obtain address using purr and map_chr using the 
    # reverseGeocode Function defined above
    Address = map_chr(geometry, reverseGeocode)
  )

For locations that don’t have points in SAM database, you can interpolate an address from the SAM Streets file.

library(tidyverse)
library(RPostgreSQL)
library(sf)

con <- DBI::dbConnect(RPostgres::Postgres(), dbname='gis', host='localhost', 
                      port=5432, user='postgres', password='xxxxx')

# function that uses PostGIS query to interpolate an address from the 
# streeet high and low adress
reverseGeocode <- function(geom) {
  # convert sf geom object to text
  geom <- st_as_text(geom)
  
  # query
  sql <- str_c("
SELECT 
  CAST (
  (leftfromaddress +
  (righttoaddress - leftfromaddress) *  ST_LineLocatePoint(ST_LineMerge(roadline), 'SRID=26918;", geom, "'::geometry)) AS integer) AS hno,
  completestreetname, leftzipname
  
  FROM (
SELECT 
  CAST(leftfromaddress AS double precision), CAST(righttoaddress AS double precision), completestreetname, leftzipname,
  \"Shape\" as roadline,
  \"Shape\" <-> 'SRID=26918;", geom, "'::geometry AS dist
FROM
  nys_sam_streets
ORDER BY
  dist
LIMIT 1)")
  
  
  dbGetQuery(con, sql)
  
  query <- dbGetQuery(con, sql)
  
  str_c(query$hno %>% as.character() %>% replace_na(''), 
        ' ',
        query$completestreetname,
        ', ',
        query$leftzipname,
        ', NY ',
        query$leftpostal
  ) %>% return
}

# obtain centroids for each county
ny.county.centroids <- tigris::county_subdivisions('ny') %>% 
  st_centroid()

# get addresses of properties by reverse gecoding
ny.county.centroids %>%
  st_transform(26918) %>%   # use same projection 
                            # as your postgis database
  mutate(
    # obtain address using purr and map_chr using the 
    # reverseGeocode Function defined above
    Address = map_chr(geometry, reverseGeocode)
  )

Why I Am Not a Libertarian or a Conservative

Many people I know giggle or maybe take a gasp when I tell them I am a liberal Democrat, or something like that. I mean, I voted for both Barack Obama in 2008 and 2012, and for Jill Stein in 2016. I did consider voting for Donald Trump in 2016, but ultimately went with Stein as protest vote against both Trump and Clinton. I voted for Senator Sanders in the Democratic primary, but honestly I wasn’t thrilled about either candidate in the primary. I thought Donald Trump would be a change, much like Obama was a change, and a fresh voice in 2008. I was very impressed with Barack Obama’s work as president, even if many of his best ideas got held up by the Republican Congress. I’m mostly indifferent to all the candidates that run for office, not liking many of them.

Pride. It comes in all colors.

I believe it’s important for government to regulate big corporations and big urban concerns, when we are talking about thousands or millions of people. As they used to say, individually people are really beautiful, but together there is a lot of collective ugliness. Most human impact is limited, but becomes more problematic when thousands or millions of humans act together – like driving automobiles in cities, generating solid waste, or using megawatts of electricity. Your 100 watt light bulb doesn’t impact shit, but burning coal to power millions of homes and oil to power millions of automobiles is producing such an incredible amount of carbon dioxide, it’s warming the planet.

I am not a believer in individual action or lifestyle statements, except for political lobbying. Replacing your incandescent bulbs with LED bulbs or buying a hybrid car is going to nothing to change the trajectory of our planet as it continues to warm. Neither will trading in your SUV for a hybrid or taking the bus is going to alone change a thing on the planet. But fuel economy standards can force manufacturers to make millions of more efficient cars, and investing in convenient transit options, can move millions in ways that reduce carbon emissions.

I promise I won't buy anymore flags

I am also not a believer in expanding social programs per se. I do see the need for universal healthcare, but it should be provided a mix of a public and private partnership. The government should be the insurer of expensive healthcare operations, while private insurers can cover ancillary needs like lost wages, premium parking, cable television or Internet at hospital rooms, or other non-essential benefits that will help to make health care pleasant. I also think college education should be highly subsidized, so students who work summer jobs and live conservatively, can afford it without going to debt.

I do not agree with welfare or providing certain individuals with special government subsidies just because they are poor – government shouldn’t be providing the poor with vochures to buy food or rental assistance. But government should work to ensure that there is an adequate supply of affordable healthy food for all regardless of income, and same with safe, decent housing for all.

At the same, I am a believer in the government creating good public places for all to use, without consideration of income. Every community should have good public spaces where people can get together and work on issues of common interest. Public libraries, free and open to all, are important in every community, so people can learn and access the Internet. Learning shouldn’t end just because you graduate from a publicly funded school. I also think parks that are free and open to all are important, as people need a chance to get some fresh air and enjoy healthy recreation. Public forest lands can provide numerous recreational opportunities from hunting to fishing to camping and hiking, and can also provide important products to industry like timber, oil, and coal – which in turn can fund their use for all.

Molon Labe

I don’t think government has much of a role regulating individual lives. I don’t support gun control, instead I think government should be working to produce healthy communities that are largely free of violence, because people have other ways to work out problems. Strong communities have good jobs, good schools, good community centers, and a mix of people that always are keeping an eye on their street. Neighborhood watches, and local anti-crime citizen groups should always be the preference over more cops and government regulation.

In urbanized areas, there needs to more regulation of private property, to protect community character, but sometimes regulation gets a little out of hand. In rural areas, there can be more freedom, because there is less of a chance of conflict. But I think there should be more review of regulations, and repeal of outdated regulations, and implementing regular sunsets of laws. It seems like there too many laws, especially on the local level, that don’t always make sense to continue.

Untitled [Expires October 31 2025]

That’s my take on it.