0
\$\begingroup\$

I have a properties file I'm using for GIS software. You have a feature like a road, then properties under it, like so:

{
    "road": {
        "colour": "rgb(0,0,0)"
     },
     "railway": {
        "colour": "rgb(100,100,100)"
     }
}

Now I need to group these into categories, for example, transportation. The first thing I thought of was:

{
    "transportation":
    {
        "road": {
            "colour": "rgb(0,0,0)"
        },
        "railway": {
            "colour": "rgb(100,100,100)"
         }
    }
 }

This makes things awkward when I want to access "road" (for example), because now I need to know it is under "transportation" (mind you I could always write a helper function to eliminate this). Further, "road" could be under multiple groups, perhaps "transportation" and "public works". Now I would need to duplicate the properties in both instances.

Would I be wiser to use the following format, or is there a better way to do this?

{
    "road": {
        "colour": "rgb(0,0,0)",
        "group": ["transportation"]
     },
     "railway": {
        "colour": "rgb(100,100,100)",
        "group": ["transportation"]
     }
}
\$\endgroup\$
  • \$\begingroup\$ This has nothing at all to do with php. \$\endgroup\$ – Theodore R. Smith Feb 5 '12 at 0:26
  • \$\begingroup\$ It has little to do with PHP. The PHP portion is stating what language I am using to work with the data. People often ask stuff like "In what application are you trying to do this in?" Part of that question is answered by having the php tag. \$\endgroup\$ – Jason McCarrell Feb 8 '12 at 20:51
2
\$\begingroup\$

Assuming you're using JavaScript to manipulate the object, you could also split it up into 2 different structures, one for maintaining the hierarchy and another for easy access:

var hierarchy = {
    transportation: {
        road: {
            colour: "rgb(0,0,0)"
        },
        railway: {
            colour: "rgb(100,100,100)"
        }
    }
};
var features = {
    road: hierarchy.transportation.road,
    railway: hierarchy.transportation.railway
};
\$\endgroup\$
  • \$\begingroup\$ I am not using javascript here, but rather straight JSON in text files. Still, this is the way I ended up leaning to. I have my styles in one JSON file, while completely ignoring the hierarchy. Then another file states the nesting hierarchy of the features. \$\endgroup\$ – Jason McCarrell Feb 8 '12 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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