Page 24 of 170

Pick the lowest and highest numbers in an array of numbers in PostgreSQL

I thought solving this problem took longer than it should have. I thought there would have been an integrated function in PostgreSQL to pick the lowest (smallest or minimum) and highest (largest or maximum) numbers in an ARRAY of numbers.

LEAST and GREATEST didn’t work, since those work on expressions, not arrays.

MIN and MAX don’t work because they are aggregating functions, and I didn’t want that.

Of course I found the solution on StackOverflow, but not after a lot of searching and trying other potential solutions.

Here it is!

Given an array of numbers, pick the lowest and highest ones using two custom functions.

CREATE OR REPLACE FUNCTION small(anyarray, int)

 RETURNS anyelement AS $$

  SELECT (ARRAY(SELECT unnest($1) ORDER BY 1 asc))[$2]

 $$ LANGUAGE sql;

The second argument in this function is to extract the Nth smallest number. In my case I want the smallest number so I set “1” for the second argument.

Example array in PostgreSQL:

{45.04,124.90,45.04,124.90}

Example query:

SELECT small(‘{45.04,124.90,45.04,124.90}’::numeric[], 1)

Output: 45.04

You can rewrite the query to select the Nth largest number by changing the “ORDER BY 1 asc” to “ORDER BY 1 desc” (reversing the order of the array’s unnesting.)

CREATE OR REPLACE FUNCTION small(anyarray, int)

RETURNS anyelement AS $$

SELECT (ARRAY(SELECT unnest($1) ORDER BY 1 asc))[$2]

$$ LANGUAGE sql;

Example array in PostgreSQL:

{45.04,124.90,45.04,124.90}

Example query:

SELECT large(‘{45.04,124.90,45.04,124.90}’::numeric[], 1)

Output: 124.9

Lock off apartments: Does zoning allow them?

These are also known as Junior Accessory Dwelling Units (JADU) in the California ADU laws. Many bungalows in Chicago were built with basement ADUs that have an exterior entrance as well as an interior entrance that can be locked off. 

At the YIMBYtown conference in Boston, Massachusetts, last week, I heard from a panel comprising a developer, an architect, and a manager of special housing projects at the City of Boston. I forget who described this novel (sort of) multi-family housing configuration, but I noted it because it has benefits similar to Chicago’s coach & rear houses.

Here’s how it works.

There would be a residential building full of condos. Each condo would have a few bedrooms. One of the bedrooms would have its own kitchen or kitchenette, bathroom, and direct entry to the building’s corridor. The bedroom would be “locked off” from the rest of the condo. 

The condo owner would rent the bedroom to a tenant, providing them housing that would most likely be less costly than an equivalent (new construction) apartment.

As the condo owner’s household changes – perhaps the family has another child – the tenant can move out and the owner can remove the kitchen to create another bedroom or closet. 

Lock offs are heavily also present in time shares. 

The zoning question is whether this condo is treated as one unit or two.

If you’re trying to increase affordable housing in your municipality, it’s necessary to classify this condo configuration as a single unit. Anything more and it wouldn’t be possible to build any of these, as the building developer would run into minimum lot area per unit and FAR limitations. 

My friend Jacob Peters quickly drew a floor plan for what a lockoff condo would look like.  

According to the speaker, the project didn’t get off the ground because the developer couldn’t get lending because of lenders who don’t understand the model. Said the speaker, “We need spaces that can evolve as our lives change. And we don’t have that flexibility in our housing stock.”

Benefits of accessory dwelling units (ADUs) like coach & rear houses

  1. Increase the supply of affordable housing
  2. Increase income for homeowners
  3. Support aging in place – ADUs give families flexibility to share property and living spaces with extended family members
  4. Increase work for small and local architects and contractors
  5. Boost local business support by restoring a neighborhood’s historical density

Update: I’m happy to see that Lennar, one of the most prolific home builders in the United States, has included the lock-off apartment in their “Next Gen” house design. A sample floor plan is below. 

Sample floor plans of Lennar’s Next Gen suburban single-family house with lock-off apartment.

Chicagoland’s massive parking footprint – as measured on September 16, 2018

Using the footprints of parking lots and garages drawn into OpenStreetMap as a data source, the area of land in Chicagoland occupied by parking lots and garages is 247,539,968 square feet. (The data was exported using HOT Export Tool; you can replicate my export.)

That converts to:

  • 5,682.71 acres
  • 8.88 mi^2 (square miles)
  • 22.99 km^2 (square kilometers)
  • ≈ 0.26 × area of Manhattan (≈ 87 km^2 )
  • 3.9% area of Chicago is parking (Chicago is ~589.56 km^2 )

(I forgot to measure the portion of this within Chicago, and now the data snapshot is gone. I fixed this in the 2019 report.)

Why Jefferson Park residents should allow more housing

Short answer: To provide more shoppers for the local businesses. Read on for the longer answer. 

Over on Chicago Cityscape I added a new feature called “market analysis” which measures the number of people who live within specific walking areas (measured by time) and driving areas (measured by distance). 

I am in favor of removing apartment & condo bans in Chicago, especially in areas where they were previously allowed and near train stations.

Jefferson Park is centered around two co-located train stations, serviced by CTA and Metra respectively. There have been multiple proposals for multi-family housing near the stations (collectively called the Jefferson Park Transit Center) and some have been approved. 

Always, however, there are residents who resist these proposals and the number of originally proposed apartments or condos gets reduced in the final version (classic NIMBYism). 

There’re four reasons – at least – why more housing should be allowed near the Jefferson Park Transit Center:

  • Locally owned businesses require a significant amount of shoppers who live nearby and walk up traffic
  • More people should have the opportunity to live near low-cost transportation
  • It will include more affordable housing, through Chicago’s inclusionary zoning rules (the Affordable Requirements Ordinance, ARO)
  • There will be less driving, and therefore lower household transportation costs and less neighborhood pollution

To support the first reason, I used the “market analysis” tool to see just how many people live in a walkable area centered around Veterans Square, a mixed-use office and retail development adjacent to the train stations. 

Only 9,368 people live within a 10 minute walk to Veterans Square (get the Address Snapshot). 

Comparatively, 19,707 people live within a 10 minute walk to The Crotch, or the center of Wicker Park, at the intersection of Milwaukee/North/Damen (get the Address Snapshot). The Blue Line station is about 75 feet south of the center point.

I would grant the low Veterans Square number a small discount based on the proximity to the Kennedy Expressway, which severely truncates walking areas up and down the northwest side. Still, even with that discount, ending up with less than half the amount as the one in Wicker Park, is disturbing. Wicker Park is hardly characterized by high-density housing. In fact, all of the new high-rises are just outside the 10 minute walk shed!

In sci-fi, even parks get turned into parking lots

In one of Philip K. Dick’s short stories, titled “Precious Artifact”, Dick appears to recognize what tends to happen in American cities. 

Earth, “Terra”, has been attacked by “Proxmen” and the “Terrans” have lost. However, one of the Terrans, who has been reconstructing Mars for future Prox inhabitation has come back to Earth. A guide meets him at the spaceport and asks the Terran where he wants to go…

“I’m Mary Ableseth, your Tourplan companion. I’ll show you around the planet during your brief stay here.” She smiled brightly and very professionally. He was taken aback. “I’ll be with you constantly, night and day.”

“Night, too?” he managed to say.

“Yes, Mr. Biskle. That’s my job. We expect you to be disoriented due to your years of labor on Mars…labor we of Terra applaud and honor, as is right.” She fell in beside him, steering him toward a parked ‘copter. “Where would you like to go first? New York City? Broadway? To the night clubs and theaters and restaurants…”

“No, to Central Park. To sit on a bench.”

“But there is no more Central Park, Mr. Biskle. It was turned into a parking lot for government employees while you were on Mars.”

“I see,” Milt Biskle said. “Well, then Portsmouth Square in San Francisco will do.” He opened the door of the ‘copter.

“That, too, has become a parking lot,” Miss Ableseth said, with a sad shake of her long luminous hair. “We’re so darn over-populated. Try again, Mr. Biskle; there are few parks left, one in Kansas, I believe, and two in Utah in the south part near St. George.”

“This is bad news,” Milt said. “May I stop at that amphetamine dispenser and put in my dime? I need a stimulant to cheer me up.”