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")
address score location.x location.y extent.xmin extent.ymin extent.xmax extent.ymax geometry
33 Livingston Avenue, New Brunswick, NJ, 08901 100.00 -74.44513 40.49297 -74.44613 40.49197 -74.44413 40.49397 POINT (-74.44513 40.49297)
Livingston Avenue, New Brunswick, NJ, 08901 97.59 -74.45771 40.48024 -74.45871 40.47924 -74.45671 40.48124 POINT (-74.45771 40.48024)
Livingston Avenue, North Brunswick, NJ, 08902 95.86 -74.47533 40.46493 -74.47633 40.46393 -74.47433 40.46594 POINT (-74.47533 40.46493)
Livingston Avenue, East Brunswick, NJ, 08816 95.86 -74.44593 40.49154 -74.44693 40.49054 -74.44493 40.49254 POINT (-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)
address score location.x location.y extent.xmin extent.ymin extent.xmax extent.ymax geometry
33 Livingston Avenue, New Brunswick, NJ, 08901 100.00 507385.6 604489.2 507107.3 604124.7 507664.0 604853.6 POINT (507385.6 604489.2)
Livingston Avenue, New Brunswick, NJ, 08901 97.59 503889.1 599850.6 503610.8 599486.2 504167.5 600215.1 POINT (503889.1 599850.6)
Livingston Avenue, North Brunswick, NJ, 08902 95.86 498988.5 594272.3 498710.2 593908.0 499266.8 594636.7 POINT (498988.5 594272.3)
Livingston Avenue, East Brunswick, NJ, 08816 95.86 507163.8 603966.6 506885.4 603602.2 507442.1 604331.1 POINT (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
Address Neighborhood City Subregion Region Postal PostalExt CountryCode Match_addr Loc_name
33 State Highway 171 New Brunswick Middlesex NJ 08901 1900 33 State Highway 171, New Brunswick, NJ, 08901 NJ_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
OBJECTID NAME GNIS_NAME GNIS ACRES SQ_MILES GLOBALID SHAPE_Length SHAPE_Area geometry
1 New Jersey State of New Jersey 1779795 5549497 8671.089 {64BFC6D2-D0A8-418C-9E76-ADF18AA40F74} 2703088 241735115122 POLYGON ((-74.67081 41.3463…

Counties

get_county_bounds() %>% 
  head()

## njgeo: downloading data
FID COUNTY COUNTY_LABEL CO GNIS_NAME GNIS FIPSSTCO FIPSCO ACRES SQ_MILES POP2010 POP2000 POP1990 POP1980 POPDEN2010 POPDEN2000 POPDEN1990 POPDEN1980 REGION SHAPE_Length SHAPE_Area geometry
1 ATLANTIC Atlantic County ATL County of Atlantic 882270 34001 1 390815.4 610.6491 274549 252552 275372 204615 450 414 451 335 COASTAL 2.054478 0.1655950 MULTIPOLYGON (((-74.67437 3…
2 BERGEN Bergen County BER County of Bergen 882271 34003 3 153490.3 239.8286 905116 884118 829592 849843 3774 3686 3459 3544 NORTHEASTERN 1.393879 0.0664519 MULTIPOLYGON (((-73.90569 4…
3 BURLINGTON Burlington County BUR County of Burlington 882272 34005 5 524903.3 820.1615 448734 423394 395066 362542 547 516 482 442 SOUTHERN 2.439422 0.2236824 MULTIPOLYGON (((-74.69864 4…
4 CAMDEN Camden County CAM County of Camden 882273 34007 7 145598.5 227.4976 513657 508932 532498 471650 2258 2237 2341 2073 SOUTHERN 1.553964 0.0619788 MULTIPOLYGON (((-75.03314 3…
5 CAPE MAY Cape May County CAP County of Cape May 882274 34009 9 183126.6 286.1353 97265 102326 95089 82266 340 358 332 288 COASTAL 1.589942 0.0772352 MULTIPOLYGON (((-74.85962 3…
6 CUMBERLAND Cumberland County CUM County of Cumberland 882275 34011 11 321150.3 501.7974 156898 146438 138053 132866 313 292 275 265 SOUTHERN 2.213656 0.1358676 MULTIPOLYGON (((-75.06186 3…

Municipalities

get_muni_bounds() %>% 
  head()

## njgeo: downloading data
OBJECTID MUN COUNTY MUN_LABEL MUN_TYPE NAME GNIS_NAME GNIS SSN MUN_CODE CENSUS2010 ACRES SQ_MILES POP2010 POP2000 POP1990 POP1980 POPDEN2010 POPDEN2000 POPDEN1990 POPDEN1980 SHAPE_Length SHAPE_Area geometry
1 CAPE MAY POINT BORO CAPE MAY Cape May Point Borough Borough Cape May Point Borough Borough of Cape May Point 885179 0503 0503 3400910330 192.0512 0.3000799 291 241 248 255 970 803 826 850 0.0415470 0.0000808 MULTIPOLYGON (((-74.95983 3…
2 WEST CAPE MAY BORO CAPE MAY West Cape May Borough Borough West Cape May Borough Borough of West Cape May 885435 0512 0512 3400978530 756.5388 1.1820919 1024 1095 1026 1091 866 926 868 923 0.0876926 0.0003182 MULTIPOLYGON (((-74.92585 3…
3 CAPE MAY CITY CAPE MAY Cape May City City Cape May City of Cape May 885178 0502 0502 3400910270 1844.8312 2.8825488 3607 4034 4668 4853 1251 1399 1619 1684 0.2031847 0.0007758 MULTIPOLYGON (((-74.8765 38…
4 WILDWOOD CREST BORO CAPE MAY Wildwood Crest Borough Borough Wildwood Crest Borough Borough of Wildwood Crest 885445 0515 0515 3400981200 947.7268 1.4808231 3270 3980 3631 4149 2208 2688 2452 2802 0.1013248 0.0003987 MULTIPOLYGON (((-74.83331 3…
5 WEST WILDWOOD BORO CAPE MAY West Wildwood Borough Borough West Wildwood Borough Borough of West Wildwood 885441 0513 0513 3400980210 232.8413 0.3638145 603 448 453 360 1657 1231 1245 990 0.0520154 0.0000980 MULTIPOLYGON (((-74.8189 39…
6 NORTH WILDWOOD CITY CAPE MAY North Wildwood City City North Wildwood City of North Wildwood 885328 0507 0507 3400953490 1593.6241 2.4900376 4041 4935 5017 4714 1623 1982 2015 1893 0.1403985 0.0006708 MULTIPOLYGON (((-74.7797 39…

Edit this page

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

Gavin Rozzi is a data scientist from New Jersey. Gavin studies urban informatics, environmental factors affecting the spread of COVID-19 & public policy development.

comments powered by Disqus