I have a topojson map that has both county and state data in json format. There are no actual names for the states, but the states do each have a unique id. I would like to find a way to return the id of a state if the user clicks on one of its containing counties.
The trouble is I cannot seem to be able to access the state ids in the json when I set up my click event listener. I have sliced and diced the json data every which way, but I keep going in circles. Is it possible to have asymmetric information within the json file? I feel like the state ids are in a black box when working with the containing counties.
Let me know if anything comes to mind. I have a fully functional minimalist example here, where I am trying to return a console log of the state id based on user click.
Note: I would prefer to avoid point in polygon solutions for complexity reasons.
Seems your json file ships with FIPS county code. That means that first two digits of county code are in fact the state code. So Math.floor(county.id / 1000) is what you‘re looking for in the end.
Related
maybe someone can help me with a overpass-turbo.eu-query.
I'd like to highlight (center of it) all cities of a country or region (or current map).
Is there maybe an "simple" example on web?
(Google was not a good friend with this special request, yet. But I am sure someone must tried to search this way already.)
Many thanks for every idea.
Here is an example for finding all cities, towns, villages and hamlets in the country Andorra:
[out:json][timeout:25];
// fetch area “Andorra” to search in
{{geocodeArea:Andorra}}->.searchArea;
// gather results
(
node[place~"city|town|village|hamlet"](area.searchArea);
);
// print results
out body;
>;
out skel qt;
You can view the result at overpass-turbo.eu after clicking the run button.
Note: When running this query for larger countries you might need to increase the timeout value. Also rendering the result in the browser might not be possible due to performance reasons. In this case use the export button and download the raw data instead.
Suppose there are 1000 Person agent on 4 (a,b, c,d) GIS region area. On a certain event, I want to communicate with all the agents living inside GIS region "a" . In "a" region we have 200 person agents.If I send message or command to Person state chart, how I can make sure that only those 200 person agents living on that specific GIS region "a" is getting my command? Is there any way to model that?
You can filter messages inside the Agent's statechart transition, in order to only executethe transition when a certain expression is true (in your case: Agent is in the right region).
Of course you could do this filtering in a lot of other places too, for example when sending the message, or when receiving it. However you can always use this code to check if the Agent is located inside of a GISRegion:
main.gisRegion1.contains(this.getLatitude(), this.getLongitude())
This is assuming you executed this inside the Agent (therefore main. and this.) and the region you are looking for is named gisRegion1.
I have am new to AS3 and coding in general so this is why I am asking this question. I am trying to make a like semi-proceduraly generated dungeon crawler sort of thing like binding of isaac using flashbuilder and flashpunk.
but this has left me with a problem; I have figured out two ways to do the rooms. Either I can spawn all the rooms at once and have the players move between them using a door entity to block progress or I can load a new world that contains the room inside it and nothing else using FP.world = new world. But this gives me two problems so basically this is the main thing I want to know.
I want the world to remember what was in it and what was killed even if I enter a new world. E.g. right now if I enter a new world it will load that world up fresh and then if I kill all the enemies and leave and then re enter it will just spawn all the enemies again. I need to know how to get that room to remember it has been cleared of enemies.
Is there anyway to do this?
I'm not familiar with FP, so I cannot tell if FP allows to save room states. You better check the documentation, but if it doesn't help, you can do the following:
Find a way to get the room unique ID somehow. It could be anything - a coordinate, a number, a name, an object, etc. Just be sure it is unique. Once you have it, proceed.
The next mandatory thing you need is a way to get and set the room state. I.e. after getting state you can know that there are three goblins in a room. And setting state would be like "remove all and put these three goblins in this room". These requirements are natural as you are going to read and write the room's content anyway. Once you have it, the rest is easy.
Choose how you will keep the state. Let's say, an Object like {goblins:3, chest:1}. If you kill one goblin, it will become { goblins:2, chest:1 }. If you want to remember just the rooms that are FULLY cleared, then you simply need a boolean variable (true/false) as a state object.
You can store the state Object in a Dictionary as a key-value pair where key is the room's unique ID.
var visited_rooms:Dictionary = new Dictionary();
var roomID = ... // unique room id
var savedState:Object = { goblins:3, chest:1 } // read room state somehow
visited_rooms[roomID] = savedState;
Now, upon leaving the room, you get the state info and put it in the Dictionary.
Once you enter this room again, just check if its unique ID is present in the Dictionary and restore the state if so:
var roomID = ... // unique room id
if (roomID in visited_rooms) {
var savedState:Object = visited_rooms[roomID];
room.setState(savedState);
}
That's basically all.
I have created a class that I've been using as the storage for all listings in my applications. The class allows me to "sign" an object to a listing (which can be created on the fly via the sign() method like so):
manager.sign(myObject, "someList");
This stores the index of the element (using it's unique id) in the newly created or previously created listing "someList" as well as the object in a 2D array. So for example, I might end up with this:
trace(_indexes["someList"][objectId]); // 0 - the object is the first in this list
trace(_instances["someList"]); // [object MyObject]
The class has another two methods:
find(signature:String):Array
This method returns an array via slice() containing all of the elements signed with the given signature.
findFirst(signature:String):Object
This method just returns the first object in a given listing
So to retrieve myObject I can either go:
trace(find("someList")[0]); or trace(findFirst("someList"));
Finally, there is an unsign() function which will remove an object from a given listing. This function basically:
Stores the result of pop() in the specified listing against a variable.
Uses the stored index to quickly replace the specified object with the pop()'d item.
Deletes the stored index for the specified object and updates the index for the pop()'d item.
Through all this, using unsign() will remove an object extremely quickly from a listing of any size.
Now this is all well and good, but I've had some thoughts which are making me consider how good this really is? I mean being able to easily list, remove and access lists of anything I want throughout the application like this is awesome - but is there a catch?
A couple of starting thoughts I have had are:
So far I haven't implemented support for listings that are private and only accessible via a given class.
Memory - this doesn't seem very memory efficient. Then again, neither is creating arrays for everything I want to store individually either. Just seems.. Larger.. Somehow.
Any insights?
I've uploaded the class here in case the above doesn't make much sense: https://projectavian.com/AviManager.as
Your solution seems pretty solid. If you're looking to modify it to be a bit more extensible and handle rights management, you might consider moving all those individually indexed properties to a value object for your AV elements. You could perform operations like "sign" and "unsign" internally in the VOs, or check for access rights. Your management class could monitor the collection of these VOs, pass them around, perform the method calls, and the objects would hold the state in a bit more readable format.
Really, though, this is entering into a coding style discussion. Your method works and it's not particularly inefficient. Just make sure the code is readable, encapsulated, and extensible and you're good.
I have UITableView representing list of cities (100 cities).
For each city I want to call specific remote(URL) JSON to get city's weather information and populate response data for each city cell in the UITableView.
When I run application, I want to see my table as fast as possible, so I don't need to wait for all json responses. I want that informations got asynchronously (when specific json is loaded, set it's information for corresponding city cell in the UITableView).
Note: It is important for me to call seperate remote JSON files.
Which technic is the best for this task?
I would start with the following approach:
Create a data structure to hold city information, including:
path to your data service,
service call "state" (idle, waiting, completed, error),
weather information (from JSON returned by service call)
When you first show the table, you will want to:
initialize your array (of the aforementioned data structure),
initiate each service call asynchronously,
set each row (city) state to waiting.
You will also probably want to return a custom UITableCellView with the city name (if you already have it) and a spinning activity indicator. This will be your best option to have a fast load time (not waiting for services to complete) and give some visual indication that the data is loading.
Each service call should use the ViewController as its delegate; you will need a key field so that when the services return, they can identify with which row/city they are associated.
As each service completes and calls the delegate, it will send the data to the ViewController, which (in turn) will update the array and initiate a UITableView update.
The UITableView update is, in my opinion, the most difficult part. Typically cells are drawn or updated when they become visible; the table pre-fetches all visible cells' geometry and then queries the actual contents when it's ready to draw each cell; as a result, your strategy for updating cells will depend on how your table is used.
If your cell geometry changes, you will most likely need to redraw your entire table; I shudder to think about what 50 simultaneous UITableView redraws will do for your app, so you might need to set a time-threshold to "chunk" updates and handle drawing more intelligently.
[theTableView reloadData] will cause the entire table to be re-queried and redrawn.
If your cell geometry does not change, you can try to be more surgical of updating only the visible cells (the non-visible ones aren't an issue since their data will be queried when they become visible).
[theTableView visibleCells] returns an array of visible cells; when your service call returns, you could update the data and then search the array to see if the cell in question is visible; if it is, you will probably need to send the specific UITableCellView a setNeedsDisplay message.
There is a good explanation of setNeedsDisplay, setNeedsLayout, and 'reloadData' at http://iosdevelopertips.com/cocoa/understanding-reload-repaint-and-re-layout-for-uitableview.html.
There is a relevant SO question at How to refresh UITableViewCell?
Lastly, you will probably want to implement some updating logic in the service delegate error routine, just so you don't create endlessly spinning activity indicators.
I do this now while searching multiple servers. I use Core Data, but you can use an NSMutableArray to accumulate your JSON responses.
Every time you finish receiving date from one of your servers (for example, when connectionDidFinishLoading executes), take the JSON data object and add it to an NSMutableArray (let's call it weatherResults) (add it using the addObject method). You may want to convert the JSON to an NSDictionary before adding it to the mutable array weatherResults.
Assuming your dataSource delegate methods refer to what is in the weatherResults NSMutableArray (for example, getting the number of rows from the size of the array using [weatherResults count]) you can do the following:
After inserting the object to the array, you can simply call reloadData in the dataSource controller. You will see the table update as each new JSON results arrives. The results should append to the bottom of the table as they come in. If you want to sort the NSMutableArray each time a JSON results arrives, you can do that too.
I do this and the time it takes to resort and reload the table is insignificant on my iPad. If you do not resort, it should be even faster.
By the way, in this explanation, I assume that the JSON response contains all of the information that you need to fill in your table cell. That may not be the case. If it's not, you will have to correlate the response with other information you have, such as a list of cities that your program is presenting.