How to make this switch statement faster? ;-)

517
2
07-26-2010 05:34 AM
AxelSchaefer
New Contributor II
This is a funny question. How to make this switch statement faster? I have the UTM hemisphere (N for North and S for South) and the UTM zone (1-60) and need to get the WKID to know from where to project my coordinates. Well, this switch statement is huge. 😉

Diggin' deeper down: I could go another way and parse the zone to the 10s number, then the 1s number, then the hemisphere. Otherwise I could use a dojox dictionary. I decided not to use a dictionary because I thought that another object in memory would be too much. Don't know the best way to handle something like this with JavaScript.

Otherwise: Writing this code was easy, copy and paste the lines directly from the JS-API documentation and change it with find/replace of regular expressions/line feeds/beginning of line/tabs.

Best regards and have a nice day.


function getUTMZone(hemi, zone) {
    if (hemi == "N") {
        switch (zone) {
            case 1:
                return 32601; // WGS_1984_UTM_Zone_1N
                break;
            case 2:
                return 32602; // WGS_1984_UTM_Zone_2N
                break;
            case 3:
                return 32603; // WGS_1984_UTM_Zone_3N
                break;
            case 4:
                return 32604; // WGS_1984_UTM_Zone_4N
                break;
            case 5:
                return 32605; // WGS_1984_UTM_Zone_5N
                break;
            case 6:
                return 32606; // WGS_1984_UTM_Zone_6N
                break;
            case 7:
                return 32607; // WGS_1984_UTM_Zone_7N
                break;
            case 8:
                return 32608; // WGS_1984_UTM_Zone_8N
                break;
            case 9:
                return 32609; // WGS_1984_UTM_Zone_9N
                break;
            case 10:
                return 32610; // WGS_1984_UTM_Zone_10N
                break;
[...]
        }
    } else if (hemi == "S") {
        switch (zone) {
            case 1:
                return 32701; // WGS_1984_UTM_Zone_1S
                break;
            case 2:
                return 32702; // WGS_1984_UTM_Zone_2S
                break;
            case 3:
                return 32703; // WGS_1984_UTM_Zone_3S
                break;
            case 4:
                return 32704; // WGS_1984_UTM_Zone_4S
                break;
            case 5:
                return 32705; // WGS_1984_UTM_Zone_5S
                break;
            case 6:
                return 32706; // WGS_1984_UTM_Zone_6S
                break;
            case 7:
                return 32707; // WGS_1984_UTM_Zone_7S
                break;
            case 8:
                return 32708; // WGS_1984_UTM_Zone_8S
                break;
            case 9:
                return 32709; // WGS_1984_UTM_Zone_9S
                break;
            case 10:
                return 32710; // WGS_1984_UTM_Zone_10S
                break;
      [...]
        }
    }
}
0 Kudos
2 Replies
__Rich_
Occasional Contributor III
Not overly familiar with the zone/wkid/projection mappings etc. but without too much thought, putting GIS/Mapping aside and thinking purely about coding, would it be safe to do something like:

return "32"  + (hemi=="N"?"6":"7") + ("0" + zone.toString()).slice(-2);


Obviously it's very hard-coded to demonstrate the principle and only works for the zones/projections you've mentioned but if that's all you need to cater for then....
0 Kudos
AxelSchaefer
New Contributor II
Sleek. I'll test it. 🙂

I had this idea also, and you're right: It relies on the relationship of WGS 84 UTM Zone number and their WKID.

Best regards
Axel
0 Kudos