New Jersey’s official geocoding API now has a client for R

I am often critical of the lack of, or inconsistent availability of administrative data from public authorities in New Jersey. But one bright spot I recently discovered has been the free geocoding service maintained by the NJ Geographic Information Network. This blog post will introduce a new side project of mine, {njgeo}, which provides an R client to New Jersey’s publicly available geocoding API to improve the workflow of analyzing spatial data specific to the state of New Jersey.

New Jersey’s official geocoding service

The New Jersey Office of GIS (NJOGIS) runs several public ArcGIS REST API endpoints via NJGIN that can be queryed by the public via the NJGIN. Many large businesses and governments rely on ESRI’s software for their GIS needs, which has led to its REST API interface becoming a de facto standard in GIS, not like Microsoft Word in the word processing space.

The state’s official geocoding service (“NJ_Geocode”) can geocode addresses in New Jersey, converting addresses into latitude and longitudes within a coordinate reference system used for mapping. The API is a convenient, free alternative to many commercial platforms that charge for geocoding by the API request (some maintained by ESRI).

One other advantage this geocoding service has is that it also takes into account some of NJOGIS’s New Jersey-specific datasets, such as the road centerlines and enhanced source address points data that they integrated.

The problem

But the problem in this case is that the ArcGIS APIs were only designed to work directly with ESRI’s software, or through manually generated user requests via their spartan web interface. There have been other third party and official clients for ArcGIS written for R, however none have been targeted specifically to work with New Jersey’s specific implmentation of the REST APIs.

ArcGIS may work for some people, but I prefer to work on GIS-related analysis in a scripting / command line environment, and being able to make requests to the ArcGIS APIs in a language like R or Python is better aligned with my preferred workflow in data science. So I decided to make this package mainly to automate some aspects of my workflow in analyzing New Jersey-specific data in R. If you happen to work with either of those things, this package may be for you.

The njgeo R package

{njgeo} is a new package for R that I recently created. This package can be used to query the state’s geocoding API without having to use ArcGIS on the desktop or use up any limited enterprise credits for geocoding. The package processes the JSON data received from the API and formulates the queries needed to perform geocoding, address candidate matchign & reverse geocoding, fully within R and without needing to rely on the use of external GIS software.

The package can be downloaded from CRAN by running install.packages('njgeo').

You can input a single string to geocode an address using {njgeo}

geocode_address_candidates("33 Livingston Ave. New Brunswick, NJ")
addressscorelocation.xlocation.yextent.xminextent.yminextent.xmaxextent.ymaxgeometry
33 Livingston Avenue, New Brunswick, NJ, 08901100.00-74.4451340.49297-74.4461340.49197-74.4441340.49397POINT (-74.44513 40.49297)
Livingston Avenue, New Brunswick, NJ, 0890197.59-74.4577140.48024-74.4587140.47924-74.4567140.48124POINT (-74.45771 40.48024)
Livingston Avenue, North Brunswick, NJ, 0890295.86-74.4753340.46493-74.4763340.46393-74.4743340.46594POINT (-74.47533 40.46493)
Livingston Avenue, East Brunswick, NJ, 0881695.86-74.4459340.49154-74.4469340.49054-74.4449340.49254POINT (-74.44593 40.49154)

The package also allows you to specify a different coordinate reference system using the package’s functions. Here we are changing from the default of EPSG:4326 to a projected one, EPSG:3424.

geocode_address_candidates("33 Livingston Ave. New Brunswick, NJ", crs = 3424)
addressscorelocation.xlocation.yextent.xminextent.yminextent.xmaxextent.ymaxgeometry
33 Livingston Avenue, New Brunswick, NJ, 08901100.00507385.6604489.2507107.3604124.7507664.0604853.6POINT (507385.6 604489.2)
Livingston Avenue, New Brunswick, NJ, 0890197.59503889.1599850.6503610.8599486.2504167.5600215.1POINT (503889.1 599850.6)
Livingston Avenue, North Brunswick, NJ, 0890295.86498988.5594272.3498710.2593908.0499266.8594636.7POINT (498988.5 594272.3)
Livingston Avenue, East Brunswick, NJ, 0881695.86507163.8603966.6506885.4603602.2507442.1604331.1POINT (507163.8 603966.6)

Batch geocoding

It is possible to batch geocode up to 1000 addresses at once using the two batch geocoding functions provided by the package.

The batch_geocode_addresses() and batch_geocode_sl() functions can batch geocode up to 1000 addresses at a time. The first function expects multiple columns of data to geocode the address, while the sl version requires an address in single column format.

Reverse geocoding

Provide a point to get matching addresses:

reverse_geocode(-74.44513, 40.49297)

## njgeo: downloading data
AddressNeighborhoodCitySubregionRegionPostalPostalExtCountryCodeMatch_addrLoc_name
33 State Highway 171New BrunswickMiddlesexNJ08901190033 State Highway 171, New Brunswick, NJ, 08901NJ_Geocode_Mul

Shape and boundary files

You can easily obtain spatial boundary data for use in projects via this package. All objects are returned as an {sf} object and a coordinate reference system can be specified via arguments to repoject the shape into a different CRS. I mainly wanted to save the time of always repeating the same API queries in some of my New Jersey-specific projects.

State

get_state_bounds()

## njgeo: downloading data
OBJECTIDNAMEGNIS_NAMEGNISACRESSQ_MILESGLOBALIDSHAPE_LengthSHAPE_Areageometry
1New JerseyState of New Jersey177979555494978671.089{64BFC6D2-D0A8-418C-9E76-ADF18AA40F74}2703088241735115122POLYGON ((-74.67081 41.3463…

Counties

get_county_bounds() %>% 
  head()

## njgeo: downloading data
FIDCOUNTYCOUNTY_LABELCOGNIS_NAMEGNISFIPSSTCOFIPSCOACRESSQ_MILESPOP2010POP2000POP1990POP1980POPDEN2010POPDEN2000POPDEN1990POPDEN1980REGIONSHAPE_LengthSHAPE_Areageometry
1ATLANTICAtlantic CountyATLCounty of Atlantic882270340011390815.4610.6491274549252552275372204615450414451335COASTAL2.0544780.1655950MULTIPOLYGON (((-74.67437 3…
2BERGENBergen CountyBERCounty of Bergen882271340033153490.3239.82869051168841188295928498433774368634593544NORTHEASTERN1.3938790.0664519MULTIPOLYGON (((-73.90569 4…
3BURLINGTONBurlington CountyBURCounty of Burlington882272340055524903.3820.1615448734423394395066362542547516482442SOUTHERN2.4394220.2236824MULTIPOLYGON (((-74.69864 4…
4CAMDENCamden CountyCAMCounty of Camden882273340077145598.5227.49765136575089325324984716502258223723412073SOUTHERN1.5539640.0619788MULTIPOLYGON (((-75.03314 3…
5CAPE MAYCape May CountyCAPCounty of Cape May882274340099183126.6286.1353972651023269508982266340358332288COASTAL1.5899420.0772352MULTIPOLYGON (((-74.85962 3…
6CUMBERLANDCumberland CountyCUMCounty of Cumberland8822753401111321150.3501.7974156898146438138053132866313292275265SOUTHERN2.2136560.1358676MULTIPOLYGON (((-75.06186 3…

Municipalities

get_muni_bounds() %>% 
  head()

## njgeo: downloading data
OBJECTIDMUNCOUNTYMUN_LABELMUN_TYPENAMEGNIS_NAMEGNISSSNMUN_CODECENSUS2010ACRESSQ_MILESPOP2010POP2000POP1990POP1980POPDEN2010POPDEN2000POPDEN1990POPDEN1980SHAPE_LengthSHAPE_Areageometry
1CAPE MAY POINT BOROCAPE MAYCape May Point BoroughBoroughCape May Point BoroughBorough of Cape May Point885179050305033400910330192.05120.30007992912412482559708038268500.04154700.0000808MULTIPOLYGON (((-74.95983 3…
2WEST CAPE MAY BOROCAPE MAYWest Cape May BoroughBoroughWest Cape May BoroughBorough of West Cape May885435051205123400978530756.53881.182091910241095102610918669268689230.08769260.0003182MULTIPOLYGON (((-74.92585 3…
3CAPE MAY CITYCAPE MAYCape May CityCityCape MayCity of Cape May8851780502050234009102701844.83122.8825488360740344668485312511399161916840.20318470.0007758MULTIPOLYGON (((-74.8765 38…
4WILDWOOD CREST BOROCAPE MAYWildwood Crest BoroughBoroughWildwood Crest BoroughBorough of Wildwood Crest885445051505153400981200947.72681.4808231327039803631414922082688245228020.10132480.0003987MULTIPOLYGON (((-74.83331 3…
5WEST WILDWOOD BOROCAPE MAYWest Wildwood BoroughBoroughWest Wildwood BoroughBorough of West Wildwood885441051305133400980210232.84130.36381456034484533601657123112459900.05201540.0000980MULTIPOLYGON (((-74.8189 39…
6NORTH WILDWOOD CITYCAPE MAYNorth Wildwood CityCityNorth WildwoodCity of North Wildwood8853280507050734009534901593.62412.4900376404149355017471416231982201518930.14039850.0006708MULTIPOLYGON (((-74.7797 39…

Edit this page

Gavin Rozzi
Gavin Rozzi
Pushing the boundaries of data, technology & public policy

Gavin Rozzi is a data scientist from New Jersey with expertise in leveraging public sector datasets, spatial data & mapping and emerging technologies to inform public policy development.

comments powered by Disqus
Next
Previous