I have a large dataset in the format below. I'm able to graph them as polygons easily enough, but am having trouble doing fine selections of the coordinates in Dataset format for further checks and processing. (I have other functions downstream that do geographic intersections that are being thrown off.) Some days I feel like I'm finally starting to understand Mathematica dataset operators, and other days I'm just confused.

ds = {{<|lat -> 49.275, lng -> -123.03|>, <|lat -> 49.2753, lng -> -123.03|>, <|lat -> 49.2753, lng -> -123.03|>, <|lat -> 49.275, lng -> -123.03|>, <|lat -> 49.275, lng -> -123.03|>},
{<|lat -> 49.275, lng -> -123.029|>, <|lat -> 49.2753, lng -> -123.029|>, <|lat -> 49.2753, lng -> -123.029|>, <|lat -> 49.275, lng -> -123.029|>, <|lat -> 49.275, lng -> -123.029|>}, 
{<|lat -> 49.275, lng -> -123.029|>, <|lat -> 49.275, lng -> -123.029|>, <|lat -> 49.2753, lng -> -123.029|>, <|lat -> 49.2753, lng -> -123.029|>, <|lat -> 49.275, lng -> -123.029|>}}
// Dataset

I have a simple check that I wanted to break out because I will probably add complexity later. I thought applying it to the dataset would be straightforward but nothing is being returned from the several configurations I've tried. I thought I was following the "Select Elements from Dataset" doc closely, but I'm still not connecting what's written there to the behaviour I'm seeing in this example. I assumed the operators would take the little mini-dataset polygon as input but I assume something else is happening instead.

testcoords[shape_] := shape[Min, "lat"] > 40 

testcoords[ds[[2]]]. (* true *)

(* returns empty Dataset *)

ds[All, Select[testcoords]] 
(* returns {} ... *)

How can I configure a selection operator to check each polygon?

  • 1
    $\begingroup$ The issue is that testcoords expects a Dataset expression, but gets a raw List instead. Compare e.g. Echo[ds[2]] with ds[2, Echo]. So the fix is to either make your function work with raw lists, or to wrap the data with Dataset beforehand, e.g. Select[testcoords@*Dataset] $\endgroup$
    – Lukas Lang
    Nov 19, 2020 at 23:10

1 Answer 1


I don't think your code is doing what you think it's doing. For example:

ds[1, 1, "lat"]
(* Missing[KeyAbsent, lat] *)

(* -123.029 *)

ds[[2]][Min, "lat"]
(* Infinity *)

The first one fails partly because you use lat in your dataset but "lat" in your testcoords and partly because I don't think Dataset is expecting a variable as a part specification so ds[1, 1, lat] also fails because lat has no value. I could be wrong, but I think you need to put quotes around all the lat and lng variables for them to work properly in a Dataset.

When you call testcoords[ds[[2]]], I think that's the same as calling ds[[2]][Min, "lat"]. I don't really understand what that means, but it's returning Infinity which I can't imagine is what you want.

Your Dataset doesn't look like any of the examples I've come across so far where each column has its own label. I'm not familiar enough with Dataset to figure out how to select items with the way you've set up your Dataset. If you can, I would recommend working with lists directly, but if you need it to be a Dataset then hopefully someone else will have a better answer.

data = {lat, lng} /. Normal[ds]
testcoords[shape_] := Min[shape[[All, 1]]] > 40
Select[data, testcoords]

Where ds is exactly as you've defined it in your question. If you have the list of associations before it's turned into a Dataset, you can drop the Normal. I find working with lists easier when it comes to graphics as well:

Graphics[Polygon/@Select[data, testcoords]]

This doesn't show anything since many of the coordinates are identical, but it is fairly straightforward. If you want the order of latitude and longitude switched, you can just do:

data = {lng, lat} /. Normal[ds]

instead, but make sure you're testing the second coordinate with testcoords.

  • $\begingroup$ I guess I'll have to keep it as a List on the Select step. Thanks for the suggestions. It's filtering properly now. $\endgroup$
    – BBirdsell
    Nov 20, 2020 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.